最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Save array of object to firebase without getting 0,1,2...as key - Stack Overflow

programmeradmin1浏览0评论

I have an array of javascript object that looks like the following.

jsObjFromCsv = 
[ 
    {
        "J251525" : {
            "APPROVER" : "[email protected]",
            "JOB DESCRIPTION " : "CLEAN THE HOUSE",
            "JOB NUMBER" : "J251525"
        }
    }, {
        "J512912" : {
            "APPROVER" : "[email protected]",
            "JOB DESCRIPTION " : "BRUSH HORSE",
            "JOB NUMBER" : "J512912"
        }
    }, {
        "J5-512" : {
            "APPROVER" : "[email protected]",
            "JOB DESCRIPTION " : "WASH CAR",
            "JOB NUMBER" : "J5-512"
            }
    } 
]

However, when I save to firebase using the following code it looks like this

saveJobToFirebase(jobs: Array<Object>) {
    const jobCodesRef = this.af.database.list('/jobCodes/' +  this.currentUserpany)
    return jobCodesRef.push(jobs);
}

I want to get rid of the 0,1,2 such that I can query end point like

jobCodes/Company1/-Kc8Q5Wuq4M91puQ_70J/J251525

-----------------My Attempt--------------

I have thought of a way that works but it does not seem to be good as explained at the end of this.

So to achieve what I wanted, I firstly change my object array to be the following

[ 
    {
        "APPROVER" : "[email protected]",
        "JOB DESCRIPTION " : "CLEAN THE HOUSE",
        "JOB NUMBER" : "J251525"

    }, {
        "APPROVER" : "[email protected]",
        "JOB DESCRIPTION " : "BRUSH HORSE",
        "JOB NUMBER" : "J512912"

    }, {
        "APPROVER" : "[email protected]",
        "JOB DESCRIPTION " : "WASH CAR",
        "JOB NUMBER" : "J5-512"

    } 
]

Then I loop through each of the object and grab a job number to get the end point and directly "SET" it to firebase with the following code

saveJobToFirebase(jobs: Array<Object>) {
    // previous code
    // const jobCodesRef = this.af.database.list('/jobCodes/' +  this.currentUserpany)
    // return jobCodesRef.push(jobs);

    // bad attempt?  
    for (let job of jobs) {
        const jobCodesRef = this.af.database.object('/jobCodes/' +  this.currentUserpany + '/' + job['JOB NUMBER']).set(job);
    }
}

And this gives me the result that I wanted.

However, there are two big problems with this method

  1. my saveJobToFirebase no longer returns a thenableReference for me to call .then at my Component. This means that I would have no way to track whether or not the action succeeded

  2. I dont know if updating firebase with for loop is a good idea? What if this JSON object has 2000 entries... I would be hammering the end point if I call it inside a for loop. It would be better if I can "push" it so that everything goes in with one request right?

I have an array of javascript object that looks like the following.

jsObjFromCsv = 
[ 
    {
        "J251525" : {
            "APPROVER" : "[email protected]",
            "JOB DESCRIPTION " : "CLEAN THE HOUSE",
            "JOB NUMBER" : "J251525"
        }
    }, {
        "J512912" : {
            "APPROVER" : "[email protected]",
            "JOB DESCRIPTION " : "BRUSH HORSE",
            "JOB NUMBER" : "J512912"
        }
    }, {
        "J5-512" : {
            "APPROVER" : "[email protected]",
            "JOB DESCRIPTION " : "WASH CAR",
            "JOB NUMBER" : "J5-512"
            }
    } 
]

However, when I save to firebase using the following code it looks like this

saveJobToFirebase(jobs: Array<Object>) {
    const jobCodesRef = this.af.database.list('/jobCodes/' +  this.currentUser.company)
    return jobCodesRef.push(jobs);
}

I want to get rid of the 0,1,2 such that I can query end point like

jobCodes/Company1/-Kc8Q5Wuq4M91puQ_70J/J251525

-----------------My Attempt--------------

I have thought of a way that works but it does not seem to be good as explained at the end of this.

So to achieve what I wanted, I firstly change my object array to be the following

[ 
    {
        "APPROVER" : "[email protected]",
        "JOB DESCRIPTION " : "CLEAN THE HOUSE",
        "JOB NUMBER" : "J251525"

    }, {
        "APPROVER" : "[email protected]",
        "JOB DESCRIPTION " : "BRUSH HORSE",
        "JOB NUMBER" : "J512912"

    }, {
        "APPROVER" : "[email protected]",
        "JOB DESCRIPTION " : "WASH CAR",
        "JOB NUMBER" : "J5-512"

    } 
]

Then I loop through each of the object and grab a job number to get the end point and directly "SET" it to firebase with the following code

saveJobToFirebase(jobs: Array<Object>) {
    // previous code
    // const jobCodesRef = this.af.database.list('/jobCodes/' +  this.currentUser.company)
    // return jobCodesRef.push(jobs);

    // bad attempt?  
    for (let job of jobs) {
        const jobCodesRef = this.af.database.object('/jobCodes/' +  this.currentUser.company + '/' + job['JOB NUMBER']).set(job);
    }
}

And this gives me the result that I wanted.

However, there are two big problems with this method

  1. my saveJobToFirebase no longer returns a thenableReference for me to call .then at my Component. This means that I would have no way to track whether or not the action succeeded

  2. I dont know if updating firebase with for loop is a good idea? What if this JSON object has 2000 entries... I would be hammering the end point if I call it inside a for loop. It would be better if I can "push" it so that everything goes in with one request right?

Share Improve this question edited Feb 4, 2017 at 15:09 Frank van Puffelen 599k85 gold badges888 silver badges858 bronze badges asked Feb 4, 2017 at 14:29 user172902user172902 3,6019 gold badges35 silver badges76 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 8

The 0, 1, 2, etc are created because you're saving an array of objects. See this blog post about arrays in Firebase for more on why this behavior exists and why Firebase recommends against storing arrays.

Calling push() will generate a so-called push ID, a value that Firebase guarantees to be unique. But since your jobs already have their own ID, this isn't needed either.

The structure you want to save, seems better: the objects each have a usable key. You could save this object with:

jsObjFromCsv =  {
    "J251525" : {
        "APPROVER" : "[email protected]",
        "JOB DESCRIPTION " : "CLEAN THE HOUSE",
        "JOB NUMBER" : "J251525"
    },
    "J512912" : {
        "APPROVER" : "[email protected]",
        "JOB DESCRIPTION " : "BRUSH HORSE",
        "JOB NUMBER" : "J512912"
    },
    "J5-512" : {
        "APPROVER" : "[email protected]",
        "JOB DESCRIPTION " : "WASH CAR",
        "JOB NUMBER" : "J5-512"
        }
};

If you watch carefully, you'll see that I've removed the array and the outermost level of objects.

Now you can save this object with:

const jobCodesRef = this.af.database.list('/jobCodes/' +  this.currentUser.company)
jobCodesRef.update(jsObjFromCsv);

The return values from update() is thennable, so you can continue when the action has completed (or failed).

For anyone interested, try to transform your array of objects to an object, with just a few lines of reduce

 const arrayToObject = (array) =>
   array.reduce((obj, item) => {
     obj[item.id] = item
     return obj
   }, {})
 const jsObjFromCsv_OBJ = arrayToObject(jsObjFromCsv)

I had similar problem but in node and no answers proved sufficient. I know your code is different but the principle is the same. Hopefully this helps someone in the future. First Firebase can save data in different ways like push or set. Push when using arrays with square brackets automatically adds in those numbers such as 0, 1, 2 so that say two people post on a blog at the same time they don't overwrite each other. Set ignores this and overwrites everything at the path to save the data as you have specified.

In my case I was adding json objects to an array with .push and getting the 0, 1, 2 and I wanted names instead of 0, 1, 2 so I basically just switched the array with an object and instead of using .push I set objects inside the one big object. To illustrate this:

What I used first:

var array = [];
jsonobject={
title:'title'
}

array.push(jsonobject);
//then I used push to firebase that array in an advanced method

The fix:

var mybigjson = {};
var nameiwantinsteadofnumbers = [insert your changing variable for each jsonobject here]
jsonobject={
title:'title'
}

mybigjson[nameiwantinsteadofnumbers]=jsonobject
//then I used push to firebase that object in an advanced method
发布评论

评论列表(0)

  1. 暂无评论