Reputation: 97
I have been meddling with the new docs for firebase which can be found here. https://firebase.google.com/docs/database/web/retrieve-data
It was stated there that in order to retrieve data this would be used.
firebase.database().ref('/users/' + userId).once('value').then(function(snapshot) {
var username = snapshot.val().username;
// ...
});
So, in my ionc app i converted it to
firebase.database().ref('/reports/' +'emergency').once('value').then(function(snapshot) {
var user= snapshot.val().name;
console.log(user)
});
Here is a screenshot of my database. My Json Tree in firebase
Problem is i just get an undefined output in the console. Anyone knows how to use and convert this code for ionic? Thanks in advance
Upvotes: 1
Views: 2790
Reputation: 97
Solved it by listening to child events
var reportRef = firebase.database().ref('/reports/' +'emergency/').orderByKey();
reportRef.on('child_added', function(data) {
console.log(data.val().email, data.val().name);
});
Console log now shows results everytime data is saved.
Upvotes: 1
Reputation: 737
There is no difference what so ever in that code when using ionic. Your problem can be solved by console logging out snapshot.val() you will see that it actually is an object. It should look similar to:
{
key1:{
address:,
email:
...
},
key2:{
address:,
email:
...},
...
}
Basically the problem is that you are calling .name on snapshot.val() which actually is a property of its child.
Upvotes: 1