检查大于1的孩子的数量 [英] Check if number of children greater than 1

查看:92
本文介绍了检查大于1的孩子的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查Firebase云端函数中某个节点的子项数是否大于1?在我的情况下,我想看看如果天数是> 1



例如:

  if('message'node的子节点> 1)
//做某事

解决方案

例如:
$ b 其中检索消息对象并计算键,例如

  var x = {1:1,A:2}; 
Object.keys(x).length; //输出2

从这里






更有利的是, code> count 节点作为消息的子节点,并在添加和删除子节点时进行监视。 Firebase提供了一个如何实现的例子: -count / functions / index.jsrel =nofollow noreferrer>这里




即可。您还可以使用 numChildren()作为快照的顶层。 numChildren



示例:

  //如果删除了喜欢的数字,请重新计算喜欢的数量
exports.countDays = functions。 onWrite(event => {
const theRef = event.data.ref;
const collectionRef = theRef.parent.child('days'); $ b (messageData.numChildren())> 1){
//做某事
}
})
});


How do I check if the number of children of a node is greater than 1 in Cloud Functions for Firebase? In my case I want to see if the number of days is > 1

For example:

if(children of 'messages' node > 1)
// do something

解决方案

Three Options, depending on your use case:

One. Retrieve the messages object and count the keys, e.g.

var x = {"1":1, "A":2};
Object.keys(x).length; //outputs 2

from here


Two. More favorable, keep a count node as a child of messages and monitor when children are added and deleted. Firebase has provided an example on how to do that:

Here


Three. You can also use numChildren() for the top level of the snapshot.

numChildren

Example:

// If the number of likes gets deleted, recount the number of likes
exports.countDays = functions.database.ref('/messages').onWrite(event => {
    const theRef = event.data.ref;
    const collectionRef = theRef.parent.child('days');
    collectionRef.once('value').then(messagesData => {
        if(messagesData.numChildren()) > 1) {
            // do something
        }
    })
});

这篇关于检查大于1的孩子的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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