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

Time:09-21

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'
})

CodePudding user response:

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'
    })
  })
})
  • Related