Save array of object to firebase without getting 0,1,2...as key
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).