user13084463
user13084463

Reputation:

How to get the children of an object in Firebase database?

I am trying to alert this array using Firebase Realtime Database:

  "discuss" : {
    "taj" : {
      "20200809" : {
        "comment" : "first comment",
        "full_date" : "2020/08/09"
      }
    }
  }

I tried to do something like this:

        data.val().children.forEach(children => {
            alert(children.comment)
        });

But came out with nothing. Does anybody have an idea?

Upvotes: 0

Views: 253

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80942

If you want to get comment from the database, then try the following:

 let ref = firebase.database().ref("dicuss");

ref.child("taj").on("value", function(snapshot) {
 snapshot.forEach(function(childSnapshot) {
  var comment = childSnapshot.val().comment;
  console.log(comment);
 });
});

Add a reference to the node taj then iterate and retrieve the comment attribute.

Check the docs:

https://firebase.google.com/docs/database/web/read-and-write

Upvotes: 1

Related Questions