As I am new to the firebase and React, I appreciate your help as I have tried everything almost.
Following is my data structure in firebase.
When I add new Beverage type i.e. Milk with its fields i.e. with nested nodes and data to Products, firebase generate child node based on random number. Please help me and let me know how I can create custom child nodes with no text as mentioned in the data structure. in actual I have ready json as mentioned below and want to add new beverage type with its details as mentioned below in json.
"Id" : 1,
"Milk" : {
"Id" : 2,
"Imported Milks" : {
"Frosted Milk" : 20,
"Milk Up" : 10
},
"Local Milks" : {
"Cow Power" : 5,
"Milk Man" : 3
}
}
following is my code:
let product = "Milk";
const dbUpdate = await firebase.database().ref("Products");
//this.state.selectedCategory is Milk
await dbUpdate.child(this.state.selectedCategory).set(
{
product:
{
Id: item.Id,
"Imported Milks": {
"Frosted Milk": 20,
"Milk Up" : 10
}
}
}
);
I just made some changes to my code. It worked but set is deleted all previous records and added only new one.
let product = "Milk";
const dbUpdate = await firebase.database().ref("Products");
//this.state.selectedCategory is Milk
await dbUpdate.set(
{
this.state.selectedCategory:{
product:
{
Id: item.Id,
"Imported Milks": {
"Frosted Milk": 20,
"Milk Up" : 10
}
}
}
}
);
Thanks