简体   繁体   中英

How to make another key instead of replacing it in firebase

I am trying to add "ProductName":"TRUE/FALSE" to my database, but when I add another (using javascript), it just removes the previous one, and adds the new one. Here is my code:

database.ref(`product-whitelist`).child(Id).set({
    [productname]: "TRUE"
});

But whenever this is run again, and "productname" has changed, it removes the previous one that was made.

If anyone knows how I can fix this, please let me know!

Try this,

database.ref(`product-whitelist\`+Id).child(productname).set({
    [productname]: "TRUE"
});

You're looking for the update() method:

database.ref(`product-whitelist`).child(Id).update({
    [productname]: "TRUE"
});

This will write the value of [productname] and leave other properties under child(Id) untouched.


The above code is equivalent to:

database.ref(`product-whitelist`).child(Id).child(productname).set("TRUE");

But this approach can only write one property. By using update you can set some properties, while leave others untouched.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM