【发布时间】:2014-12-06 22:22:48
【问题描述】:
代码:
Template.chapterForm.rendered = ->
Meteor.startup ->
currentChapter = Chapters.findOne(
_id: "Wdyxj8pWg2HPzuDuf"
)
console.log currentChapter
console.log currentChapter.content
我得到以下输出:
未定义
来自跟踪器的异常 afterFlush 功能:无法读取未定义类型错误的属性“内容”:
无法读取未定义的属性“内容”
我很困惑。是 CoffeScript 错误还是 Meteor/mongodb 错误?
(我尝试将代码移动到文件顶部。仍然未定义)
编辑:
奇怪,如果我这样做了
Chapters.findOne({_id: "Wdyxj8pWg2HPzuDuf"});
在控制台中我确实得到了对象:
Object
【问题讨论】:
-
发生这种情况是因为在第一页渲染时数据还没有到达客户端,所以集合是空的。稍后 websocket 连接获取其余数据,调用返回一个对象。
-
这是流星编程中最常见的陷阱之一。请参阅this post 以获得完整的解释和解决方案。
标签: javascript mongodb meteor coffeescript