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

267
Visualizações
How to remove sub child after filtering from Realtime firebase with indexOn

I want to remove expired users in cat01.So i want to filter date from nodes and remove that user node only. Following is my firebase structure.

firebase Structre

I tried this below code .It shows successfully .but the node is not deleted.

deleteData: function (req, callback, res) {
     {
     let userId = req.User;
    
     const db = firebase.database()
  
     let usersRef = db.ref("Foreign/").child(userId).child("Category").child("Cat01")
     usersRef 
       .equalTo(uid)
       .once("value")
       .then(function (snapshot) {
         snapshot.forEach((childSnapshot) => {
         usersRef.child(childSnapshot.key).remove();
         });
       });

Method 2 i tried this way too .but it remove whole user from UserID. But i just want to remove uid="u001" when date is expired.it is successful until cat01.i think error cause because of index. May i know any method to access index there .

 deleteData: function (req, callback, res) {
        {
        let userId = req.User;
       
        const db = firebase.database()
     
    
        let usersRef = db.ref("Foreign/").child(userId).child("Category").child("Cat01")
        usersRef.remove()
              .then(function() {
                console.log("succeeded!")
              })
              .catch(function(error) {
                console.log("failed: ",error)
              });
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You're missing an orderBy... call in your query here:

usersRef 
   .equalTo(uid)
   .once("value")
   ...

To order/filter on the uid property, use:

usersRef 
   .orderByChild("uid") // 👈
   .equalTo(uid)
   .once("value")
   ...

To allow the above query to work efficiently, you'll need to add an index to your security rules:

{
  "rules": {
    ...
    "Foreign": {
      "$userId": {
        "Category": {
          "$categoryId": {
            ".indexOn": "uid"
          }
        }
      }
    }
  }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Accourding to the structure in your Realtime you still need to go a farther child which is 'user'.
But if you want to get on multiple children for Cat01 that also have multiple children you can't do that, because Realtime pagination works only with one level of multiple children

So you have to do this:

let usersRef = db.ref("Foreign/").child(userId).child("Category").child("Cat01").child('user')

Or if you have multiple children like 'user', you have to do each one in a separate query.

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