【问题标题】:Meteor Collection (subscribe and publish) unexpected returnsMeteor Collection(订阅和发布)意外返回
【发布时间】:2014-02-21 04:25:10
【问题描述】:

这就是我在 app 文件夹根目录下的 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) 

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

【问题讨论】:

    标签: javascript meteor


    【解决方案1】:

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

    lib/collections/polls.js

    Polls = new Meteor.Collection('polls');
    

    不过,我对您的问题有些困惑 - 我假设该集合实际上称为 polls,而发布标识符是 polls_all(这些是非常不同的东西)。

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

    【讨论】:

    • 在我完成之后,如果我之前不需要 Meteor.subscribe,我应该把meteor.subscribe 放在哪里?它只会出现在waitOn 函数中吗?然后我可以在整个路线中使用它吗?或者它是如何工作的?
    • 这取决于你想做什么。如果您想始终订阅该集合,您只需将Meteor.subscribe(...) 添加到任何文件(最好是一个名为client/subscriptions.js 之类的文件)。另一方面,如果您只想为此路线加载数据,我会将其保存在waitOn 中。延迟订阅通常有利于提高性能,但由您决定什么是有意义的。
    • @DavidWeldon 这对我有用,但是在将 iron:router 添加到我的应用程序后,集合文件被多次加载
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-30
    • 2016-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    • 2013-11-18
    相关资源
    最近更新 更多