0

This is what I have saved in DB enter image description here

This is my code which I used to try to access the data.

The goal is to get the value of the 1st id

var ref = db.ref("server/events/data");
ref.once("value", function(snapshot) { 

  var ids = snapshot.val();
  console.log(ids.id);

});

What am I missing ?

1
  • Please post the full exception details Commented Jun 11, 2017 at 6:34

2 Answers 2

2

You should specify the index to access

  var ref = db.ref("server/events/data");
    ref.once("value", function(snapshot) { 

      var ids = snapshot.val();
      console.log(ids[0].id);

    });
Sign up to request clarification or add additional context in comments.

1 Comment

@Suhaib you can use for loop or forEach for example check Frank answer
1

Alternative to Dinesh' answer is looping over all results:

var ref = db.ref("server/events/data");
ref.once("value", function(snapshot) { 
  snapshot.forEach(function(childSnapshot) {
    var value = childSnapshot.val();
    console.log(value.id);
  });
});

Or accessing only a specific index:

var ref = db.ref("server/events/data/0");
ref.once("value", function(snapshot) { 
  var value = snapshot.val();
  console.log(value.id);
});

1 Comment

Awesome. That's what I was looking for. Thanks for looking beyond my specific question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.