2016-11-07 25 views
2

我有一个流星发布函数(在服务器端代码中),它返回两个不同集合中的两个光标的数组。 在每种情况下,返回的游标都应包含满足某些查询条件的文档,这些查询条件作为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); 

有一个登录按钮,设置所谓的“用户id”的会话。 控制台记录当前的id,但记录到控制台的文档始终为空(虽然它存在)。

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

+1

由于代码不在上下文中,所以稍微难以分辨发生了什么,但这通常是因为订阅还没有准备好。如果你在浏览器中用'accounts.find()'在行上放置一个断点,然后让它在暂停后再次运行,现在它可以工作,那么这就是你的问题。 –

回答

0

将您的客户端代码取决于回调中的订阅。

//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); 

});