Alessandro
Alessandro

Reputation: 89

I don't know how to update an entry by it's unique id

![This is how the DB looks like ]1enter image description hereI have this app that gets an item from the firebase real time DB and displays it nicely but I am not able to edit the item via the app :(

I mean I need somehow to get the unique ID generated when inserting an item ..

For the display part I use Object.entries(data).map((item, index) => (item.key = index));

Upvotes: 0

Views: 63

Answers (1)

Ruslan
Ruslan

Reputation: 134

https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot.html#key

  1. Get the whole object with the key
let retrievedItem;
const items = firebase.database().ref(itemsPath);
items.once('value').then(
  snapshot => snapshot.forEach(item => {
    retrievedItem = {id: item.key, ...item.val()};
  })

https://firebase.google.com/docs/reference/js/firebase.database.Reference.html#update

  1. Update the item
items.child(retreivedItem.id).update(updatedObject);

Upvotes: 1

Related Questions