这就是我在应用程序文件夹根目录下的router.js文件中获得的内容
this.route('pollyShow', {
path: '/polly/:_id',
template: 'polly_show',
notFoundTemplate: 'notFound',
waitOn : function () {
return Meteor.subscribe('polls_all');
},
before: function () {
Meteor.subscribe('polls_all');
var Polls = new Meteor.Collection('polls_all');
console.log(Polls);
var id = this.params._id;
var poll = Polls.findOne({_id: id});
console.log(poll);
},
});
我在我的server/server.js文件中得到了这个文件
Meteor.publish('polls_all', function () {
return Polls.find({});
});
我在我的console.log中得到以下错误
console.log(Polls)
返回以下Meteor.Collection {_makeNewID: function, _transform: null, _connection: Connection, _collection: LocalCollection, _name: "polls_all"…}
这显然不是我所期望的。
然后console.log(poll)
会像预期的那样返回undefined
,因为console.log(Polls)
返回的是完全没有预料到的东西。
最后,我得到了以下错误。
Exception from Deps recompute: Error: There is already a collection named 'polls_all'
at new Meteor.Collection (http://localhost:3000/packages/mongo-livedata.js?32cb8e7b8b1a4ecda94a731b3a18e434e3067a5f:263:13)
at route.before (http://localhost:3000/router.js?f11d1e915689f0ca34e03814eb5acc76c7d2d798:15:16)
at IronRouteController.runHooks (http://localhost:3000/packages/iron-router.js?7a9e077ee92fd60193e5d33532c9c2406c28cb5b:649:12)
at Utils.extend.run (http://localhost:3000/packages/iron-router.js?7a9e077ee92fd60193e5d33532c9c2406c28cb5b:2024:10)
at null._func (http://localhost:3000/packages/iron-router.js?7a9e077ee92fd60193e5d33532c9c2406c28cb5b:1484:22)
at _.extend._compute (http://localhost:3000/packages/deps.js?eba25ec453bb70e5ae5c2c54192d21e0e4d82780:183:12)
at _.extend._recompute (http://localhost:3000/packages/deps.js?eba25ec453bb70e5ae5c2c54192d21e0e4d82780:196:14)
at _.extend.flush (http://localhost:3000/packages/deps.js?eba25ec453bb70e5ae5c2c54192d21e0e4d82780:288:14)
我对publish
和subscribe
方法非常陌生,因为我一直在使用autopublish
包。我正在尝试转换,我遇到了很多困难。
发布于 2014-02-21 04:25:11
你需要定义你的集合一次。在上面的代码中,每次运行pollyShow
路由时都会定义Polls
。鉴于这是一个将在客户端和服务器上共享的集合,您应该在应用程序根目录下的共享目录中添加定义,如下所示:
lib/collections/polls.js
Polls = new Meteor.Collection('polls');
我对你的问题有点困惑--我假设这个集合实际上叫做polls
,而发布标识符是polls_all
(这是非常不同的东西)。
您也不需要在before
钩子中添加Meteor.subscribe('polls_all');
,因为您已经在等待它了。
https://stackoverflow.com/questions/21918557
复制相似问题