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
2 Answers
Reset to default 8Your 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
版权声明:本文标题:javascript - I want to get the data from a firebase object how? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741225660a2361807.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论