Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

256
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!