代码:为什么下面的Meteor/mongodb findOne返回undefined?
Template.chapterForm.rendered = ->
Meteor.startup ->
currentChapter = Chapters.findOne(
_id: "Wdyxj8pWg2HPzuDuf"
)
console.log currentChapter
console.log currentChapter.content
我得到以下输出:
undefined
Exception from Tracker afterFlush function: Cannot read property 'content' of undefined TypeError:
Cannot read property 'content' of undefined
我不解。它是CoffeScript错误还是Meteor/mongodb错误?
(我试过的代码移动到文件的顶部仍然得到不确定的。)
编辑:
奇怪,如果我做
Chapters.findOne({_id: "Wdyxj8pWg2HPzuDuf"});
在我做的控制台获取对象:
Object
发生这种情况是因为在第一页上呈现数据并未到达客户端,因此该集合为空。稍后,websocket连接将获取剩余的数据,并且调用返回一个对象。 – imslavko 2014-10-12 04:23:12
这是流星编程中最常见的陷阱之一。请参阅[本文](https://dweldon.silvrback.com/guards)获取完整的解释和解决方案。 – 2014-10-12 04:37:00
[Meteor findOne query可能重复在一个模板助手中返回undefined。在其他模板助手中,相同的查询效果不错](http://stackoverflow.com/questions/17728386/meteor-findone-query-returns-undefined-in-one-template-helper-in-other-template) – 2015-03-26 00:41:14