Within my firebase DB, I have the following data stored:
{
"vehicles" : {
"fz20tqpxUChOM98fNUYGQhtZ83" : {
"amount" : 31,
"timeStamp" : "2017-07-18T20:31:34Z"
},
"sw30tqpxUChOM98fNUrGQhtk33" : {
"amount" : 45,
"timeStamp" : "2017-07-18T20:31:34Z"
}
}
To retrieve a snapshot of this data, I am using the .on
method:
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 'undefined' 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 ===");
}
}
});
When attempting to access the keys using console.log(obj.key);
, I consistently receive undefined
as the output. However, when specifically targeting obj.fz20tqpxUChOM98fNUYGQhtZ83
, I am able to retrieve the corresponding data. This issue raises questions about why the key values are not being displayed despite attempts to do so programmatically.