【问题标题】:Making subscriptions with arguments in meteor在流星中使用参数进行订阅
【发布时间】:2016-11-07 17:21:26
【问题描述】:

我有一个流星发布函数(在服务器端代码中),它返回来自两个不同集合的两个游标的数​​组。 在每种情况下,返回的游标都应该包含满足某些查询条件的文档,这些条件作为 Meteor.publish 中函数的参数给出。 下面的代码会更清楚:

//server
Meteor.publish('publisher', function(userId){
return[
    posts.find({createdBy: userId}),
    accounts.find({_id: userId})
    ];
});
//client
Meteor.subscribe('publisher',Session.get('userId'));
//this code runs within a meteor method on the client
var id = Session.get('userId');
console.log(id)
var acnts = accounts.find({_id: id}).fetch();
console.log(acnts);

有一个登录按钮,用于设置名为“userId”的会话。 控制台记录当前 id,但记录到控制台的文档始终为空(尽管它存在)。

任何帮助将不胜感激。 :)

【问题讨论】:

  • 由于代码与上下文无关,因此很难判断发生了什么,但这通常会发生,因为订阅尚未准备好。如果你在浏览器中与accounts.find() 所在的行放了一个断点,然后在暂停后让它再次运行,现在它可以工作了,那么这就是你的问题。

标签: meteor publish-subscribe


【解决方案1】:

将依赖于订阅的客户端代码放入回调中。

//server
Meteor.publish('publisher', function(userId){
return[
    posts.find({createdBy: userId}),
    accounts.find({_id: userId})
    ];
});

//client
Meteor.subscribe('publisher',Session.get('userId'),function(){
    //this code runs within a meteor method on the client
    var id = Session.get('userId');
    console.log(id)
    var acnts = accounts.find({_id: id}).fetch();
    console.log(acnts);

});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-17
    • 2016-02-17
    • 2012-10-01
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多