Ahmad
Ahmad

Reputation: 39

How to update value directly in firebase realtime database inside dynamic key

I'm trying to set "pretend" value using the following code but it didn't work.

dataBase.ref().orderByChild('en_word').equalTo('pretend').set({
  en_word: 'Pretend'
})

Upvotes: 0

Views: 1239

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600006

Writing a value to the Firebase requires that you know the complete, exact path where you want to write. It doesn't support so-called update queries, where you send a query and a write operation to the database in one go. Instead you will have to:

  1. Execute the query.
  2. Loop over the results.
  3. Update each of them in turn.

So in your case that'd be something like:

let query = dataBase.ref().orderByChild('en_word').equalTo('pretend');
query.once("value").then((results) => {
  results.forEach((snapshot) => {
    snapshot.ref.update({
      en_word: 'Pretend'
    })
  })
})

Upvotes: 1

Related Questions