mur7ay
mur7ay

Reputation: 833

Retrieving Data From Firebase Not Displaying Correctly in Console

I'm able to display the correct data from my Firebase child in the console with the following code:

var ref = firebase.database().ref('requests');
ref.on('value', gotData, errData);


function gotData(data) {
  var scores = data.val();
  console.log(scores);

}

Which displays the following in the console:

enter image description here

enter image description here

So when I try to use this snippet of code to retrieve the email:

function gotData(data) {
  var scores = data.val();
  console.log(scores);
  var keys = Object.keys(Email);
  console.log(keys);
  for (var i = 0; i < keys.length; i++) {
    var k = keys[i];
    var emails = email[k].email;
    console.log(emails);
  }
}

it's displaying the following error:

enter image description here

but I'm not fully understanding why I'm getting this error when this is very similar to other peoples solution.

Anyone know why I'm getting this error when there is an actual value for the email key?

Upvotes: 0

Views: 313

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80914

To get the email simply to do this:

var ref = firebase.database().ref('requests');
ref.on('value', function(snapshot) {
snapshot.forEach(function(child) {
var datas = child.val();
var email=child.val().Email;

  });
});

Assuming you have this database:

requests
   randomid 
       Email: email_here 

You need to be iterating inside the randomid to be able to access the property Email

Upvotes: 1

Related Questions