如何在 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 : "abc@gmail.com"
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
下的数据。
关于javascript - 如何在 firebase web 中获取新添加的子项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55678271/