A. Ilazi A. Ilazi - 1 month ago 10
Javascript Question

How to write dynamic data in firebase3 as a child node?

Hello everyone I am trying to write data to my firebase database. I have multiple "codes" that can be generated by the user and should be shown as seperate nodes inside a child node.

firebase.database().ref('users/').push({
user: uid,
titel: title,
codes: {
for(var i = 0;i < arrCodes.length;i++){
shareCode[i]: true,
}
},
enable: true,
});


the important part should look like this:

...
titel: title,
codes: {
xksodkcisd: true,
ksocjdiuvn: true,
// and so on
},
enable: true,
...

Answer

Move the loop outside the .push()?

var shareCodes = {};

for (var i = 0; i < arrCodes.length; i++) {
    shareCodes[i] = true;
}

firebase.database().ref('users/').push({
    user: uid,
    titel: title,
    codes: shareCodes,
    enable: true,
});