Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在firebase web中获取新添加的子项?_Javascript_Firebase_Firebase Realtime Database - Fatal编程技术网

Javascript 如何在firebase web中获取新添加的子项?

Javascript 如何在firebase web中获取新添加的子项?,javascript,firebase,firebase-realtime-database,Javascript,Firebase,Firebase Realtime Database,如何在firebase中获得新添加的节点值? 我想获取集合中新添加的子节点 我尝试过这个,但它给了我最后一个孩子的值: var starCountRef = firebase.database().ref('testingchat/'); starCountRef.on('child_added', function (snapshot) { console.log("abc"); console.log(snapshot.ref.parent.key); consol

如何在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数据库中标识用户


您应该避免按说明嵌套数据库,将数据库更改为以下内容:

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
下的数据