【问题标题】:Cloud Functions for Firebase - numChildren()Firebase 的云函数 - numChildren()
【发布时间】:2017-06-03 13:58:39
【问题描述】:

我想获取快照的子节点数。

收据里面是不同的收据:

/receipts/{receiptid}/ -> 收据id,用户(...)

let bookID = "book1" // e.g. -> String from another function

let ref = admin.database().ref("books").child(bookID).child("receipts");
ref.once("value", function(snapshot){

//get number of children of snapshot
let number = snapshot.numChildren(); //DOESN´T WORK -> ERROR

});

【问题讨论】:

  • “不起作用”是指number 的值不是您所期望的吗?是零吗?
  • 不,在我的 firebase 控制台中只给出一个错误,指出 numChildren() 未定义
  • 这很奇怪。我刚刚在云函数中运行了您的代码,没有出现错误。
  • 我用了 let ref = admin.databaser().ref("books").child(bookID).child("receipts") .. 这有什么改变吗?
  • 编辑您的帖子以显示产生错误的实际代码。当前代码未显示您如何定义bookID

标签: javascript firebase google-cloud-functions


【解决方案1】:

numChildren必须在key之前,例如:

JSON:

{
    people { 
       name: "val", 
       age: "10" 
   }
}//people have 2 children.

因此,要获得您需要的孩子数量:

firebase.database().ref().child ("people").once("value").then(snap =>
    console.log (snap.numChildren());
    //the return will be: 2
);

【讨论】:

    猜你喜欢
    • 2019-01-18
    • 2017-12-24
    • 2017-08-02
    • 2017-08-22
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    • 2017-07-31
    • 2019-10-17
    相关资源
    最近更新 更多