Firebase在我的node.js应用程序中没有正常运行
Firebase not behaving as it should in my node.js app
Firebase在我的node.js应用程序中表现得非常古怪,我不知道为什么。
例如,如果我有一个看起来像这样的变量:
var AllItems= {"itemList":[{"id":1,"name":....}
我可以记录它并让它正确地吐出所有内容,但如果我尝试通过内部数组获取它,就像这样。
var itemList = AllItems[itemList]
返回undefined
。
如果我试着在这样的循环中一次吐出每一个:
for(i in AllItems){
console.log('i: ',i)
}
它吐出这样的东西:
i: 0
i: 1
i: 2
i: 3
一直到i: 7581
,我只有13个条目,假设它记录在那里的索引号,但不是,它应该只输出一个itemList
,其中上面的[{"id":1,"name":....
将是它的值。
我的火球模块是
var Firebase = require("firebase");
var FirebaseTokenGenerator = require("firebase-token-generator");
我想您的问题是不了解如何正确访问AllItem对象。
访问一个项目列表
在您的问题中,您尝试以AllItems[itemList]
的方式访问您的项目列表。其中,itemList
被认为是一个变量,即undefined
。并且AllItems[undefined]
是未定义的。
以下是访问它的两种方法:
var itemList = allItems['itemList'];
var itemList = allItems.itemList;
项目列表上的循环
我不知道你是如何到达i: 7581
的,但我就是这样做的:
for(var key in allItems) {
console.log(allItems[key]);
}
这会记录allItems中的每个itemList。
代码段
我把刚才解释的每一件事都做了一个小片段。
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]);
}
相关文章:
- 仅为一个窗体运行应用程序脚本
- 谷歌图表在运行应用程序时只能显示一次
- MAC操作系统-如何通过Java类运行应用程序(.app)
- 在安装后首次运行应用程序时从 UIBebView 发出 jQuery Ajax 请求
- 离子应用程序在浏览器上运行,但在我的安卓设备中无法正常工作
- Firebase在我的node.js应用程序中没有正常运行
- 如何更新日期?当我运行应用程序时,它被卡住了
- 如何从 HTML 运行应用程序
- 使用 WebView.loadUrl 执行 javascript 在第一次运行应用程序时不起作用
- 不知道如何在节点中运行应用程序.js
- Node.js以及如何在应用程序中获得正常运行时间/启动时间
- 如何在运行应用程序时用javascript清除AngularJS应用程序中的缓存
- 搜索源阻止运行应用程序后,从流星1.1更新到流星1.2
- 如果我应用断点并在调试模式下运行应用程序,则应用程序可以完美运行
- 我在本地主机上运行应用程序时出现错误.无法加载XMLHttpRequest
- 你的意思是js:垂直模块运行应用程序创建器
- 在node.js中运行应用程序时出现连接超时错误
- 错误:$injector:modulerr模块运行应用程序时出错
- Knockout只有在Heroku上运行应用程序时才能解析绑定
- 多次运行应用程序而不停止上一次