Heather Butterfield
Heather Butterfield

Reputation: 11

Firebase snapshot has no val attribute

I'm kind of new to firebase and I'm running into a problem when I try to retrieve data from the database. My database structure looks like this:

/users
-> 12345
    -> name: Jane Doe
    -> email: [email protected]

When I try to get the name with the following code, I am not getting a snapshot.val as you can see in the screenshot here

Here's my code:

firebase.database().ref('/users/12345').once('value').then(function(snapshot) {
    console.log(snapshot)
});

What am I doing wrong?

Upvotes: 0

Views: 341

Answers (1)

Supuna Warusawithana
Supuna Warusawithana

Reputation: 904

You can't get name value just using snapshot. Snapshot is a complex object.

Try this code.

firebase.database().ref('/users/12345').once('value').then(function(snapshot) {
console.log(snapshot.val()['name'])
});

If you want to retrieve any child data then You can pass the attribute name inside [].

Upvotes: 1

Related Questions