I am new to Firebase, and I am trying to figure out how to retrieve data from my Firebase database. I was able to add to it successfully based on a user's unique id (uid), but now getting data from it seems to be incredibly difficult. I want to grab the first name from the current user ('user' in the following code), but this JavaScript doesn't seem to be working:
var uid = user.uid
firebase.database().ref('users/' + uid).on('value', function(snapshot) {
this.first_name = snapshot.val().first_name;
});
As soon as I make the call to this.first_name, it gives me the following error:
FIREBASE WARNING: Exception was thrown by user callback. TypeError: Cannot set property 'first_name' of null.
In case it helps, my database is structured like this:
{
users: {
"uid1": {
first_name: "John"
},
"uid2": {
first_name: "Sue"
}
}
}
this.first_name. What are you expectingthisto be? Since you're in the middle of a callback chain, thethiscontext may be a variety of things (includingnull).