简体   繁体   English

如何根据子 Cloud 函数删除 Firebase 中的某个父节点

[英]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 实时数据库结构

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.我想要做的是能够遍历每个具有特定 feedId 的循环,我一直在尝试这种方式,但目前没有运气。 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
  })
}

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:最简单的解决方法是使用Promise.all()等待所有删除完成:

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) => {
      updates[snap.key] = null;
    })
    return ref.update(updates);
  })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Firebase云函数中经过特定日期后,如何删除子节点? - How to remove a child node after a certain date is passed in Firebase cloud functions? 如何在Firebase中定位某个Node的子节点? - How do I target the child of a certain Node in firebase? Firebase的云功能:如何为所有子项的特定子键触发数据库功能 - Cloud Functions for Firebase: How do I trigger a database function for a specific child key of all children 在Node.js中访问Firebase子节点-Firebase云功能 - Access Firebase Child Node in Node.js - Firebase Cloud Functions 如何在Firebase Cloud Functions中删除包含嵌套子节点的子节点? - How to delete a child node including nested child nodes in Firebase Cloud Functions? 如何让孩子进入Firebase上的亲子内部? - How do I get the child to inside a parent child on Firebase? Firebase根据子值删除节点 - Firebase remove node based on child value 为什么在Firebase云功能中不会删除此子节点? - Why does this child node not get removed in Firebase Cloud Functions? 在这种情况下,如何避免使用Firebase Cloud Functions进行承诺嵌套? - How do I avoid promise nesting in this case with Firebase Cloud Functions? 如何使用 Firestore 在 Cloud Functions for Firebase 中获取服务器时间戳? - How do I get the server timestamp in Cloud Functions for Firebase with Firestore?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM