Reputation: 242
I have a JSON database setup on Google Firebase and I have an Angular 6 App that is looking to post an object to that database. Everything works fine except there is an alphanumeric name appended to my list item and I can't get it off.
The database looks like this:
{
"thursdayPanels": {
"panel": [
{
"tagline": "tagline 1",
"content": "some content 1",
"id": 1
},
{
"tagline": "tagline 2",
"content": "some content 2",
"id": 2
},
{
"tagline": "tagline 3",
"content": "some content 3"
"id": 3
}
]
}
}
The update function looks like this:
addPanel(panelInfo){
return this.http.post(`${this.baseURL}/thursdayCampaign/panel.json`, panelInfo).subscribe((po:Response) => {console.log("po",po)})
where panel info is an object structured like this, that gets passed to the addPanel() function:
this.panelInfo ={
"week": panel.id,
"tagline": panel.tagline,
"content": panel.content,
}
When the panel gets uploaded to firebase it looks like this (please dont mind the name differences):
I couldn't find any firebase database documentation that would tell me why it is doing this or how to stop it from doing that. It's preventing the *ngFor functions I pass this data to from working my app.
Thanks in advance.
Upvotes: 0
Views: 90
Reputation: 317412
According to the REST API documentation for Realtime Database, when you do a POST, that "pushes" data into the database in the same way that the JavaScript push() method works. It always generates a random ID and adds the data under it. If you don't want this behavior, then you should instead construct the full path of the node to add, and PUT that in the path of your URL.
If you were hoping that the POST was going to automatically find the next available number in the list, that's not possible. That's actually not how Firebase lists work at all. The proper way to manage a lists of data is with push()
, accepting the random generated IDs.
Sequential numeric indexes are not a scalable way to manage data in Realtime Database. You might want to read more about why arrays are evil in Firebase, and adopt a different way of modeling your data.
Upvotes: 1