Reputation: 325
I'm currently working on retrieving a child from Firebase with this code:
function fetchUserdetails() {
firebase.database().ref().child('login/').orderByChild('username').equalTo('ivanaldwin').on("value", function(snapshot) {
document.getElementById('fullname').innerHTML = snapshot.val().username;
console.log(snapshot.val()); //debug in console
snapshot.forEach(function(data) {
console.log(data.key);
alert('yep im working dood');
});
});
}
window.onload = fetchUserdetails();
But the code, if i use snapshot.val().username;
will render undefined
in the output. but if i use snapshot.val()
only, the output will be [Object object]
. Here's my firebase layout:
Upvotes: 0
Views: 1099
Reputation: 1660
You need to use array index
document.getElementById('fullname').innerHTML = snapshot.val()[0].username;
But I think you would need the element 0 for that one, which you don't have. Maybe try
document.getElementById('fullname').innerHTML = snapshot.val().child('1').username
Upvotes: 0
Reputation: 80914
To retrieve the username try the following:
snapshot.forEach(function(data) {
let userName = data.val().username;
console.log(data.key);
alert('yep Im working dude');
});
Your snapshot is at node login
, therefore after you loop using forEach
you then can access the child attributes.
Upvotes: 2