简体   繁体   中英

Update key in Firebase

I've been going through the docs, but I don't see a way to update a value at a specific key (while keeping the rest). Is the only way to achieve this is to fetch the existing data and overwrite it using set()?

I have something like this:

firebase.database()
  .ref(`/users/${currentUser.uid}/products/${productKey}`)
    .set({category: name});

where products have many other key-value pairs, and using set() overwrites the entire object, which is not what I want in this case.

I appreciate any help!

From the section Update specific fields :

To simultaneously write to specific children of a node without overwriting other child nodes, use the update() method.

In your snippet that would be:

firebase.database()
  .ref(`/users/${currentUser.uid}/products/${productKey}`)
  .update({category: name});

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