Home > front end >  How to remove sub child after filtering from Realtime firebase with indexOn
How to remove sub child after filtering from Realtime firebase with indexOn

Time:10-29

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

CodePudding user response:

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.

CodePudding user response:

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") //            
  • Related