Firebase的云功能 - numChildren()

时间:2017-06-03 13:58:40

标签: javascript firebase google-cloud-functions

我想获得快照的子女数量。

收据内部是不同的收据:

/收据/ {receipttid} / - > recetid,user(...)

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

});

1 个答案:

答案 0 :(得分:0)

numChildren必须在密钥之前,例如:

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
);