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

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 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!