Reputation: 13
How can I get newly added node value in firebase? I want to get the newly added child node in my collection .
I've tried this, but it gives me the last child values:
var starCountRef = firebase.database().ref('testingchat/');
starCountRef.on('child_added', function (snapshot) {
console.log("abc");
console.log(snapshot.ref.parent.key);
console.log("abc");}
As you can see in picture I want values from 3 including the '3' so I can use this to identify users from my sql database.
Upvotes: 1
Views: 100
Reputation: 80914
You should avoid nesting the database as explained here, change your database to the following, :
testingchat
3
email : "[email protected]"
profile_picture: "abc"
username : "ok"
then do the following:
let ref = firebase.database().ref("testingchat");
ref.once("value", function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var key = childSnapshot.key;
var childData = childSnapshot.val();
});
});
Here key
property will retrieve 3
and childSnapshot.val()
will retrieve the data under 3
.
Upvotes: 1