Reputation: 8585
Firebase is acting really funky in my node.js app and I don't know why.
For instance if I have a varible that looks something like this:
var AllItems= {"itemList":[{"id":1,"name":....}
I can log it and have it spit out everything correctly but if I try get it by the inner array, like this.
var itemList = AllItems[itemList]
It returns undefined
.
All if I try spit out each one at a time in a loop like this:
for(i in AllItems){
console.log('i: ',i)
}
It spits out something like this:
i: 0
i: 1
i: 2
i: 3
all the way to i: 7581
and I only have 13 Items assuming it is logging there index number but its not and it should be spitting out just that one itemList
where the above [{"id":1,"name":....
would be its value.
My firebase modules are
var Firebase = require("firebase");
var FirebaseTokenGenerator = require("firebase-token-generator");
Upvotes: 0
Views: 35
Reputation: 1430
I guess your problem is that you do not understand how to access your AllItem object properly.
In your question you try to access your itemList this way AllItems[itemList]
. There, itemList
is considered a variable, which is undefined
. And AllItems[undefined]
is undefined.
Here are the two ways to access it:
var itemList = allItems['itemList'];
var itemList = allItems.itemList;
I do not know how you get to i: 7581
but this is how I did it:
for(var key in allItems) {
console.log(allItems[key]);
}
This logs every itemList in allItems.
I made a little snippet with everything I just explained.
var allItems = {
"itemList": [{
"id": 1,
"name": "item1"
}, {
"id": 2,
"name": "item2"
}, {
"id": 2,
"name": "item2"
}]
};
// Log itemList (2 ways)
console.log(allItems['itemList']);
console.log(allItems.itemList);
var itemList = allItems.itemList;
// Log every item in itemList
itemList.forEach(function (item) {
console.log(item);
});
// Log every itemList in allItems
for(var key in allItems) {
console.log(allItems[key]);
}
Upvotes: 1