这是我的数据库结构
{
"main" : {
"fake data1" : {
"abc" : "1001",
"high" : "100",
"low" : "150",
"mid" : "200",
"name" : "fake data1"
},
"fake data2" : {
"abc" : "1023",
"high" : "1020",
"low" : "1020",
"mid" : "1501",
"name" : "fake data2"
},
"fake data3" : {
"abc" : "1001",
"high" : "100",
"low" : "150",
"mid" : "200",
"name" : "fake data3"
},
"fake data4" : {
"abc" : "1023",
"high" : "1020",
"low" : "1020",
"mid" : "1501",
"name" : "fake data4"
},
"fake data5" : {
"abc" : "1001",
"high" : "100",
"low" : "150",
"mid" : "200",
"name" : "fake data5"
},
"fake data6" : {
"abc" : "1023",
"high" : "1020",
"low" : "1020",
"mid" : "1501",
"name" : "fake data6"
}
}
}
我想从上面检索所有名字,但我无法做到。
我正在尝试使用以下查询:
var datafire = firebase.database().ref();
var namedb = datafire.child('main')
var query = namedb.orderByChild('name')
query.on("value", function(datasnap) {
var data = (datasnap.val());
console.log(data.name);
答案 0 :(得分:1)
对Firebase数据库执行查询时,可能会有多个结果。因此快照包含这些结果的列表。即使只有一个结果,快照也会包含一个结果的列表。
您的代码需要使用Snapshot.forEach()
循环这些项来处理快照包含多个项目这一事实:
var datafire =firebase.database().ref();
var namedb = datafire.child('main')
var query = namedb.orderByChild('name')
query.on("value",function(datasnap){
datasnap.forEach(function(child) {
var data=child.val();
console.log(child.key, data.name);
});
});