Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

117
Visualizações
Updating Data in Firebase using React

I am updating an object in firebase using React js. I'm using this boilerplate as reference.

  updateBookList: (id, data) => {
    return firebaseDb.ref('NewBooks').child(id).update(data).then(() => {
      return {};
    }).catch(error => {
      return {
        errorCode: error.code,
        errorMessage: error.message
      }
    });
  },

The following updates the Books fine.

What I want to do is return the result instead of returning a blank {}. How can I return the result of what I updated?

This is how I fetch books:

  fetchBooks: () => {
    return new Promise((resolve, reject) => {
      const bookSub = firebaseDb.ref('NewBooks').on("value", books => {
        resolve(books.val());
      }, error => {
        reject(error);
      })
    })
  },
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

If you want to return the value, you need to retrieve it. You can do that using once and the value event, which returns a promise that resolves to a Firebase snapshot:

updateBookList: (id, data) => {
  let ref = firebaseDb.ref('NewBooks');
  return ref
    .child(id)
    .update(data)
    .then(() => ref.once('value'))
    .then(snapshot => snapshot.val())
    .catch(error => ({
      errorCode: error.code,
      errorMessage: error.message
    }));
}

Also, you could simplify your fetchBooks by using once there, too:

fetchBooks: () => {
  return firebaseDb.ref('NewBooks')
    .once("value")
    .then(snapshot => snapshot.val());
}

once returns a promise, so you don't have to create your own and you won't have a dangling event listener. The call to on in your implementation of fetchBooks will see a listener added with each call and multiple calls to resolve will be attempted if the database changes.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda