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

167
Visualizações
Remove specific node from Firebase in JavaScript

how do I remove this specific node in my database as shown in this picture below? (the nickname will be different since I'm just testing it, if anyone confuses) enter image description here

I've tried

    const roomRef = firebase.database().ref(`allRooms/${rooms}`);

    roomRef.orderByChild('nickname').equalTo(`${username}`).once('value',(snapshot)=> {  
                   snapshot.ref.remove();  })

But it just delete all of my nodes in that room, thank you

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You're almost there.

When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result. And your code needs to handle the fact that `` is a list with something like:

roomRef.orderByChild('nickname').equalTo(username).once('value',(snapshot)=> {
  snapshot.forEach((userSnapshot) => {  
    userSnapshot.ref.remove();  
  })
})

The above runs a separate remove() call for each matching node. If you have only one (or a few) nodes with the same name, this is probably fine. But if you have more of them, consider using a single multi-path update to remove all nodes like this:

roomRef.orderByChild('nickname').equalTo(username).once('value',(snapshot)=> {
  let updates = {};
  snapshot.forEach((userSnapshot) => {  
    updates[userSnapshot.key] = null; // 👈 setting to null will remove the node
  })
  roomRef.update(updates); // 👈 send all updates in one call
})
about 4 years ago · Juan Pablo Isaza Relatório

0

Here snapshot.ref will be roomRef itself therefore using remove() method on that will remove whole allRooms/roomId node. To delete that specific child node, you need a reference to that. Try this:

const roomRef = firebase.database().ref(`allRooms/${rooms}`);

roomRef.orderByChild('nickname').equalTo(`${username}`).once('value').then((snapshot) => {  
  if (snapshot.val()) {
    const keys = Object.keys(snapshot.val())
    snapshot.ref.child(keys[0]).remove().then(() => {
      //         ^^^^^ deleting that child instead of roomRef
      console.log("Data Deleted")
    }).catch(e => console.log(e));  
  }
})
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