【发布时间】:2017-07-18 22:34:59
【问题描述】:
我的 firebase 数据库中有这个:
{
"vehicles" : {
"fz20tqpxUChOM98fNUYGQhtZ83" : {
"amount" : 31,
"timeStamp" : "2017-07-18T20:31:34Z"
},
"sw30tqpxUChOM98fNUrGQhtk33" : {
"amount" : 45,
"timeStamp" : "2017-07-18T20:31:34Z"
}
}
我正在使用.on 来获取数据的快照:
var ref = database.ref('vehicles');
ref.on('value', function(snapshot) {
var obj = snapshot.val();
for (var key in obj){
console.log(obj);//prints my obj
console.log(obj.key);//prints 'undefind' both times (why??)
console.log(obj.fz20tqpxUChOM98fNUYGQhtZ83);//prints data
if ('fz20tqpxUChOM98fNUYGQhtZ83' === key) {//Just for testing
console.log("IT IS ==="); //my 'key' is equal to
//the hardcoded key
} else {
console.log("NOT ===");
}
}
});
当我 console.log(obj.key); 时,两个键都得到 undefined。但是,当我运行console.log(obj.fz20tqpxUChOM98fNUYGQhtZ83); 时,我得到了我的数据,显然我不能在那里对我的密钥进行硬编码,所以为什么当我问它时它也不打印???
【问题讨论】:
标签: javascript json database firebase firebase-realtime-database