首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Meteor Collection (subscribe和publish)意外返回

Meteor Collection (subscribe和publish)意外返回
EN

Stack Overflow用户
提问于 2014-02-21 04:15:38
回答 1查看 1.7K关注 0票数 0

这就是我在应用程序文件夹根目录下的router.js文件中获得的内容

代码语言:javascript
运行
复制
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文件中得到了这个文件

代码语言:javascript
运行
复制
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)返回的是完全没有预料到的东西。

最后,我得到了以下错误。

代码语言:javascript
运行
复制
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) 

我对publishsubscribe方法非常陌生,因为我一直在使用autopublish包。我正在尝试转换,我遇到了很多困难。

EN

回答 1

Stack Overflow用户

发布于 2014-02-21 04:25:11

你需要定义你的集合一次。在上面的代码中,每次运行pollyShow路由时都会定义Polls。鉴于这是一个将在客户端和服务器上共享的集合,您应该在应用程序根目录下的共享目录中添加定义,如下所示:

lib/collections/polls.js

代码语言:javascript
运行
复制
Polls = new Meteor.Collection('polls');

我对你的问题有点困惑--我假设这个集合实际上叫做polls,而发布标识符是polls_all (这是非常不同的东西)。

您也不需要在before钩子中添加Meteor.subscribe('polls_all');,因为您已经在等待它了。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21918557

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档