如何在firebase中获取新添加的节点值?我想在我的集合中获取新添加的子节点。
我试过这个,但它给了我最后一个孩子的价值观:
var starCountRef = firebase.database().ref('testingchat/');
starCountRef.on('child_added', function (snapshot) {
console.log("abc");
console.log(snapshot.ref.parent.key);
console.log("abc");}
正如您在图片中看到的,我想要3中的值,包括'3',因此我可以使用它来识别我的sql数据库中的用户。
您应该避免按照here的说明嵌套数据库,将数据库更改为以下内容:
testingchat
3
email : "[email protected]"
profile_picture: "abc"
username : "ok"
然后执行以下操作:
let ref = firebase.database().ref("testingchat");
ref.once("value", function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var key = childSnapshot.key;
var childData = childSnapshot.val();
});
});
这里key
属性将检索3
和childSnapshot.val()
将检索3
下的数据。