使用 firebase for Web 检索数据
Retrieving data with firebase for Web
我正在尝试从 firebase 数据库中检索数据:
firebase.database().ref("/appointments").orderByChild("doctor").equalTo(doctorId).on("value", function(snapshot) {
var appointmentsData = snapshot.val();
for(var appointment in appointmentsData) {
if (!appointmentsData.hasOwnProperty(appointment)) continue;
var obj = appointmentsData.appointment;
}
});
如果我 console.log appointment 或 console.log appointmentsData,我得到正确的值,但如果我 console.log appointmentsData.appointment,我得到未定义。
知道如何从 firebase returns 的对象中检索属性和值吗?
您想使用 Firebase
的内置 forEach
功能。它允许您遍历快照并轻松获取该对象内每个 属性 的键和值。它可以是另一个对象或一个平面值。
firebase.database().ref("/appointments").orderByChild("doctor").equalTo(doctorId).on("value", function(snapshot) {
snapshot.forEach(function(childSnapshot) {
// key
var key = childSnapshot.key;
// value, could be object
var childData = childSnapshot.val();
// Do what you want with these key/values here
...
});
});
参考:
我正在尝试从 firebase 数据库中检索数据:
firebase.database().ref("/appointments").orderByChild("doctor").equalTo(doctorId).on("value", function(snapshot) {
var appointmentsData = snapshot.val();
for(var appointment in appointmentsData) {
if (!appointmentsData.hasOwnProperty(appointment)) continue;
var obj = appointmentsData.appointment;
}
});
如果我 console.log appointment 或 console.log appointmentsData,我得到正确的值,但如果我 console.log appointmentsData.appointment,我得到未定义。
知道如何从 firebase returns 的对象中检索属性和值吗?
您想使用 Firebase
的内置 forEach
功能。它允许您遍历快照并轻松获取该对象内每个 属性 的键和值。它可以是另一个对象或一个平面值。
firebase.database().ref("/appointments").orderByChild("doctor").equalTo(doctorId).on("value", function(snapshot) {
snapshot.forEach(function(childSnapshot) {
// key
var key = childSnapshot.key;
// value, could be object
var childData = childSnapshot.val();
// Do what you want with these key/values here
...
});
});
参考: