admin管理员组

文章数量:1278653

Okay, am using firebase I tried some code from the docs that show me how to retrieve data from my DB. Am using the Web Docs Okay Here is what my DB looks like

 MyApp-446464
 users
    foo
     -foobar
       first_name:"foo"
       last_name:"bar"

    bar
     -barfoo
       first_name:"bar"
       last_name:"foo"

So from the above, I get the object using

firebase.database().ref('users/').once('value').then(function(snapshot) {
   var username = snapshot.val();
});

But the problem is I can't seem to get the value if I use

username.first_name;

its giving me undefined can someone assist me with what am doing wrong

Okay, am using firebase I tried some code from the docs that show me how to retrieve data from my DB. Am using the Web Docs Okay Here is what my DB looks like

 MyApp-446464
 users
    foo
     -foobar
       first_name:"foo"
       last_name:"bar"

    bar
     -barfoo
       first_name:"bar"
       last_name:"foo"

So from the above, I get the object using

firebase.database().ref('users/').once('value').then(function(snapshot) {
   var username = snapshot.val();
});

But the problem is I can't seem to get the value if I use

username.first_name;

its giving me undefined can someone assist me with what am doing wrong

Share Improve this question edited Sep 13, 2017 at 14:28 leocabrallce 3222 silver badges9 bronze badges asked Jul 12, 2016 at 14:55 RedcodeRedcode 731 gold badge2 silver badges9 bronze badges 2
  • why do you have you user info under two level childs? /users/id1/id2 – adolfosrs Commented Jul 12, 2016 at 14:57
  • The first one is the uid and the other created from a push @adolfosrs – Redcode Commented Jul 12, 2016 at 14:58
Add a ment  | 

2 Answers 2

Reset to default 8

Your code is asking for the entire list of users. So it must handle that list in the callback:

firebase.database().ref('users/').once('value').then(function(snapshot) {
    snapshot.forEach(function(userSnapshot) {
        var username = userSnapshot.val();
        console.log(username.first_name);
    });
});

This wont be possible having /users/uid/id2.

Make sure you save your user data using ref.child('users').child(uid).set(userDataObject).

Then you will be able to retrieve the data using orderByChild and equalTo. You can use this question as reference to achieve it.

var ref =  firebase.database().ref();
ref.child('users').orderByChild('first_name').equalTo(username.first_name).once('value' ...

本文标签: javascriptI want to get the data from a firebase object howStack Overflow