我正在关注此文档:
https://www.firebase.com/docs/web/api/datasnapshot/foreach.html
基本上我复制这样的例子:
var ref = new Firebase("https://FIREBASEID.firebaseio.com/users");
ref.once("value", function(snapshot) {
// The callback function will get called twice, once for "fred" and once for "barney"
snapshot.forEach(function(childSnapshot) {
// key will be "fred" the first time and "barney" the second time
var key = childSnapshot.key();
console.log(key);
});
});
与您期望得到的示例不同
> Fred
> Barney
我改为
> First
> Last
> First
> Last
上面每个名字中的键都是。
我可以通过将firebase ref更改为:
来修复它var ref = new Firebase("https://FIREBASEID.firebaseio.com/");
为什么这不像文档一样工作?
答案 0 :(得分:2)
文档还提供了数据结构:
{
"users": {
"fred": {
"first": "Fred",
"last": "Flintstone"
},
"barney": {
"first": "Barney",
"last": "Rubble"
}
}
}
您尚未提供正在使用的数据结构。答案是你创建的数据结构并不是你真正想要的那样。它可能与此类似:
{
"users":[{
"First": "Fed",
"Last": "Flintstone"
},
{
"First": "Barney",
"Last": "Rubble"
}]
}
当你可能意味着:
{
"users":{
"Fred": {
"First": "Fred",
"Last": "Flintstone"
},
"Barney": {
"First": "Barney",
"Last": "Rubble"
},
}
}