【发布时间】:2015-06-30 00:15:56
【问题描述】:
我完成了 Meteor 的“简单待办事项”教程,并认为我已经了解了它的工作原理。但是,我现在开始自己的项目,并立即遇到问题。
无论我尝试什么,我都无法从 Meteor 中获取任何数据并进入我的模板,甚至无法进入 console.log。总是undefined。我什至在终端中通过meteor mongo 仔细检查了集合,并且我传入的数据是正确的。我什至尝试过硬编码文档 ID。
我希望有人能指出我的代码明显有什么问题。
这是当前不起作用的迭代,但我似乎已经尝试了我在网上找到的所有变体:
JS:
Pros = new Mongo.Collection("pros");
if (Meteor.isClient) {
Meteor.subscribe("pros");
Router.configure({
layoutTemplate: 'main'
});
Router.route('/brief/:_id', function () {
this.render('Brief', {
data: function(){
var proid = this.params._id;
return Pros.findOne({ _id: proid });
}
});
});
}
if(Meteor.isServer){
Meteor.publish("pros", function () {
return Pros.find();
});
}
HTML:
<template name="main">
<h1>Title here</h1>
{{> yield}}
</template>
<template name="brief">
<h2>{{ name }}</h2>
</template>
(我也在 pros 的 foreach 中尝试过上述方法)
数据库数据(通过终端中的查找命令)
db.pros.find({});
{ "_id" : ObjectId("55915761145c29018ac97edb"), "name" : "Jimbob" }
我现在还尝试了以下方法,但没有产生任何不同的结果(它只是保持加载状态):
Pros = new Mongo.Collection("pros");
if (Meteor.isClient) {
Router.configure({
layoutTemplate: 'main'
});
Router.route('/brief/:_id', {
loadingTemplate: 'loading',
waitOn: function () {
return Meteor.subscribe('pros', this.params._id);
},
action: function () {
this.render('brief');
}
});
}
if(Meteor.isServer){
Meteor.publish('pros', function(proId) {
return Pros.findOne(proId);
});
}
有什么想法吗?
【问题讨论】:
-
请同时显示您的模板和/或您用于打印到控制台的代码。
-
另外,您使用的是哪个路由器?铁路由器?
-
是的,铁路由器 - 我现在将粘贴一些模板代码,但我认为它不会增加讨论:)
-
在模板代码中添加,我还粘贴了另一个我尝试过但没有更好结果的解决方案(直接来自铁路由器文档)。
-
通过蒙古atmospherejs.com/msavin/mongol查看您的订阅状态
标签: mongodb meteor iron-router