Umer Nasir
Umer Nasir

Reputation: 13

how to get newly added child in firebase web?

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.

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

Answers (1)

Peter Haddad
Peter Haddad

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

Related Questions