【问题标题】:What is wrong with my subscription method on my route?我的路线上的订阅方法有什么问题?
【发布时间】:2014-12-18 00:01:06
【问题描述】:

有人能看出为什么路由没有订阅该出版物吗? Profiles = new Meteor.Collection('profiles');

mongo 数据库在此集合中确实有文档,但浏览器控制台在 Profiles 集合中的计数仍然为 0。

我试图告诉路由器,“订阅用户配置文件发布,当你准备好时,呈现'配置文件'模板。我还将路由命名为'配置文件'。”

现在我注意到在输入sub = Meteor.subscribe('user-profile');sub.ready(); 之后,我得到了集合的计数。否则不订阅路径。这种行为以前没有发生过。

lib/router.js

Router.plugin('loading', {loadingTemplate: 'Loading'});

Router.route('user/profile', {
name: 'profile',
waitOn: function () {
 // return one handle, a function, or an array
 return Meteor.subscribe('user-profile');
 },

action: function () {
 // this.ready() is true if all items returned from waitOn are ready
 if (this.ready())
 this.render('profile');
 else
 this.render('Loading');
}
});

server.js:

Meteor.publish('user-profile', function () { 
return Profiles.find({userId: this.userId});
});

userId 是 Profiles 集合中的一个字段。此配置文件 doc id 存储在 user.profile.experiences 数组中以供参考。

【问题讨论】:

    标签: meteor iron-router


    【解决方案1】:

    Meteor.userId 是一个返回 _id 的函数,而不是 _id 本身,并且无论如何您都不能通过 DDP 传递函数。应该是:

    waitOn: function () {
      return Meteor.subscribe('user-profile', Meteor.userId());
    }
    

    【讨论】:

    • 所以在发布函数中,我应该返回 Profiles.find({}, {$in: {userId: Meteor.user.profile().experiences}}); // 这引用了 Profile doc id 的数组,然后删除了我传递给 pub/sub 的参数?
    • @CodeCandy,你在 profile 集合中的 userId 变量中存储了什么?
    • 我将 Meteor.userId() 存储在方法中。
    • 为了参数,我删除了所有参数并发布了所有类似这样的文档// Profiles.find({}); // 这仍然不起作用
    • 与 pub/sub 一样,使用 Mongo shell 检查服务器上是否有文档,使用控制台检查客户端上是否有文档,尝试在控制台中运行 sub = Meteor.subscribe... 并检查sub.ready() 看看你的订阅是否真的准备好了。这些都在文档中。
    猜你喜欢
    • 1970-01-01
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    • 2014-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多