我是Meteor的新手,我几个小时都遇到了这个问题。
我已经在一个文件' sessions.js'中创建了一个集合。在imports / lib / sessions.js
import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
Sessions = new Mongo.Collection('sessions');
我使用浏览器控制台在数据库中创建了一些文档。然后我想使用模板助手返回一个文档:
import { Template } from 'meteor/templating';
import { Sessions } from '../../../../imports/lib/sessions.js';
Template.CarCamping.helpers({
test: function() {
return Sessions.findOne({name: 'David'});
}
});
这会引发以下错误:
debug.js:41Exception in template helper: TypeError: Cannot read property 'findOne' of undefined
at Object.test (http://localhost:3000/app/app.js?hash=e301d24dd47ccd4c73ae01a856896ad67acaaca0:77:32)
at http://localhost:3000/packages/blaze.js?hash=ef41aed769a8945fc99ac4954e8c9ec157a88cea:2994:16
at http://localhost:3000/packages/blaze.js?hash=ef41aed769a8945fc99ac4954e8c9ec157a88cea:1653:16
at http://localhost:3000/packages/blaze.js?hash=ef41aed769a8945fc99ac4954e8c9ec157a88cea:3046:66
at Function.Template._withTemplateInstanceFunc (http://localhost:3000/packages/blaze.js?hash=ef41aed769a8945fc99ac4954e8c9ec157a88cea:3687:12)
at http://localhost:3000/packages/blaze.js?hash=ef41aed769a8945fc99ac4954e8c9ec157a88cea:3045:27
at Spacebars.call (http://localhost:3000/packages/spacebars.js?hash=65db8b6a8e3fca189b416de702967b1cb83d57d5:172:18)
at Spacebars.mustacheImpl (http://localhost:3000/packages/spacebars.js?hash=65db8b6a8e3fca189b416de702967b1cb83d57d5:106:25)
at Object.Spacebars.mustache (http://localhost:3000/packages/spacebars.js?hash=65db8b6a8e3fca189b416de702967b1cb83d57d5:110:39)
at ._render (http://localhost:3000/app/app.js?hash=e301d24dd47ccd4c73ae01a856896ad67acaaca0:30:22)
我已经尝试过了:
事情是:当我使用' Sessions.findOne({name:' David'})从控制台查询数据库时,'它确实给了我预期的结果。但是从代码本身来看,它无法正常工作。
非常感谢帮助!提前谢谢。
答案 0 :(得分:1)
您未从Sessions
导出imports/lib/sessions.js
,因此当您转到import
时,Sessions
将无法定义。试试这个:
import { Mongo } from 'meteor/mongo';
export const Sessions = new Mongo.Collection('sessions');
您可以在Todos
集合here中看到类似的模式。