Google Firebase: snap.val().username returns undefined

Viewed 400

I get the item in the realtime database where the email child equals the email in the localStorage and that works just fine.

however when i try to log the username, it returns undefined instead of the actual username

let useremail = localStorage.getItem("useremail")

    firebase.database().ref("/users").orderByChild('email').equalTo(useremail).on("value", function(snap){
        let data = snap.val();
        console.log(data.username)
    })

Anyone knows how to fix this?

database looks like this

database looks like this

If i log data this shows: enter image description here

and If i log useremail the correct string does show

1 Answers

Try this:

let useremail = localStorage.getItem("useremail")

firebase.database().ref("/users").orderByChild('email').equalTo(useremail).on("value", function(snap){
snap.forEach(function(childSnapshot)){
    let data = childSnapshot.val();
    console.log(data.username)
 });
});

Since reference is at node users then you need to loop to be able to access the attributes.

Related