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

257
Visualizações
How do I remove a certain parent node in Firebase based on a child Cloud functions

I currently have my DB structure like this firebase realtime database structure

What I want to do is be able to loop thorugh each one that has a certain feedId, I've been trying it this way but am currently having no luck. Would anyone be able to assist?

function clearAllPostsInOwnFeed(userId) {
  return admin
  .database()
  .ref(constants.FBC_USERS_FEEDS + '/' + userId)
  .once('value')
  .then((snapshot) => {
    snapshot.forEach((snap) => {
      if (snap.child('fromId').val() === userId) return snap.remove()
      return snap
    })
    console.log(snapshot)

    return snapshot
  })
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You're currently returning a snapshot from the inner callback. Since that is not a promise, it resolves right away - and your function gets terminated. before it has deleted the nodes.


The simplest fix is to use Promise.all() to wait for all deletes to finish:

function clearAllPostsInOwnFeed(userId) {
  return admin
  .database()
  .ref(constants.FBC_USERS_FEEDS + '/' + userId)
  .once('value')
  .then((snapshot) => {
    let promises = []
    snapshot.forEach((snap) => {
      if (snap.child('fromId').val() === userId) {
        promises.push(snap.ref.remove())
      }
    })
    return Promise.all(promises);
  })
}

Alternatively you can use a multi-path update to wipe all matching nodes with a single write:

function clearAllPostsInOwnFeed(userId) {
  const ref = admin
  .database()
  .ref(constants.FBC_USERS_FEEDS + '/' + userId);

  return ref
  .once('value')
  .then((snapshot) => {
    let updates = {};
    snapshot.forEach((snap) => {
      if (snap.child('fromId').val() === userId) {
        updates[snap.key] = null;
      }
    })
    return ref.update(updates);
  })
}
about 4 years ago · Juan Pablo Isaza 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