有超过1个孩子的节点没有被删除? [英] Node with more than 1 child is not getting removed?

查看:169
本文介绍了有超过1个孩子的节点没有被删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有多于一个孩子的天节点不会被移除。我该如何解决这个问题?

以下是我的代码(最初来自

解决方案

为了使您的代码正常工作,只需替换下面一行: -

  return collectionRef; 
collectionRef.once('value')。then(messagesData => {

如下: -

pre $ return collectionRef.once('value')。then(messagesData => {

原因在于中的return语句返回collectionRef 进一步的代码从执行。而不是,你必须返回承诺(行动),如在我建议的替换完成。

这应该现在解决您的问题,但正如弗兰克·范·普菲伦(Frank van Puffelen)在评论中提到的那样,学习承诺的概念将在未来帮助你!

A 'days' node with more than 1 child isn't getting removed. How can I fix this issue?

Here's my code below (originally from here):

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const defaultDatabase = admin.database();

exports.deleteOldItems = functions.database.ref('/path/to/items/{pushId}')
.onWrite(event => {
  var ref = event.data.ref.parent; // reference to the items
  var now = Date.now();
  var cutoff = now - 2 * 60 * 60 * 1000;
  var oldItemsQuery = ref.orderByChild('timestamp').endAt(cutoff);
  return oldItemsQuery.once('value', function(snapshot) {
    // create a map with all children that need to be removed
    var updates = {};
    snapshot.forEach(function(child) {
      updates[child.key] = null
    });
    // execute all updates in one go and return the result to end the function
    return ref.update(updates);
  }).then(function() {;

    const theRef = event.data.ref;
    const collectionRef = theRef.parent.child('days');
    return collectionRef;
    collectionRef.once('value').then(messagesData => {
        if(messagesData.numChildren() > 1) {

  let updates = {};
updates['/days'] = null;
return defaultDatabase.ref().update(updates); // 'days' doesn't get removed even if it has more than 1 child (as in the image)!
        }
    })
});

});

Data structure:

解决方案

To make your code work, just replace the following line :-

return collectionRef;
collectionRef.once('value').then(messagesData => {

with the following :-

return collectionRef.once('value').then(messagesData => {

The reason this works is that the return statement in return collectionRef prevents further code from executing. Instead of that, you must return the promise(action) as is being done in my suggested replacement.

This should solve your problem right now, but as Frank van Puffelen mentioned in his comments, learning the concepts of promises would tremendously help you in the future!

这篇关于有超过1个孩子的节点没有被删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆