2015-02-12 55 views
2

我在Meteor.Users集合中添加了一个名为usertype的附加字段。在Iron-router中,如果用户登录,我将返回用户对象。现在,在我的模板中,我需要检查此usertype字段是否存在,否则,我将用户指向用户注册屏幕。Meteor.User不加载初始加载的所有字段

这里发生的事情是,即使我的发布函数中有usertype字段,用户对象最初不会返回该字段。它仅在2-3个对象加载后显示。这会混淆模板加载逻辑,因为该字段在初始加载时未找到,但实际上该字段存在。

DashboardController = RouteController.extend({ 
template: 'dashboard', 
subscriptions: function() { 
    this.userProfileSub = Meteor.subscribe('singleUser', this.myId()); 
}, 
myId: function() { 
    var userId = Meteor.userId(); 

    if(!userId) 
     { userId = ''; } 

    return userId; 
}, 
user: function() { 
    return Meteor.users.findOne(this.myId()); 
}, 
data: function() { 
    var user = this.user(); 
    console.log(user); 

    if(user) 
    { 
     return { 
      user: this.user(), 
      ready: this.userProfileSub, 
     };   
    } 
} 
}); 

这里是发布方法:

Meteor.publish('singleUser', function(id) { 
check(id, String); 
return Meteor.users.find({_id:id}, {fields: { emails: 1, 
              profile: 1, 
              usertype: 1, 
              "services.facebook.id": 1, 
              "services.facebook.email": 1, 
              "services.twitter.screenName": 1, 
              "services.twitter.profile_image_url": 1, 
              "services.google.email": 1, 
              "services.google.picture": 1}}); 
}); 

编辑 如下给出,如果订阅移动waitOn内阻止它应该等待订阅完全加载答案。

waitOn: function() { 
    this.userProfileSub = Meteor.subscribe('singleUser', this.myId()); 
    return [this.userProfileSub]; 
}, 

但现在当我尝试等待多个订阅加载到等待数组中时,它显然不会等待所有这些订阅。我仍然得到空阵列。尽管我检查了动作块。我可以从我的控制台稍后找到数据。

waitOn: function() { 
    return [ 
      Meteor.subscribe('singleUser', this.myId()), 
      Meteor.subscribe('singleAgentByUserId', this.myId()), 
      Meteor.subscribe('singleClientByUserId', this.myId())]; 
}, 

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

你可以发表你的发布方法吗? =) – ahren 2015-02-12 06:20:46

+0

@ahren添加了发布方法 – srinivas 2015-02-12 06:33:34

回答

0

更新EDITED(查看修订历史太大的不同版本,由于原来的问题)

尝试:

waitOn: function() { 
    return [ 
     Meteor.subscribe('singleUser', Meteor.userId()), 
     Meteor.subscribe('singleAgentByUserId', Meteor.userId()), 
     Meteor.subscribe('singleClientByUserId', Meteor.userId()]; 
}, 
loadingTemplate: "loading", 
action: function() { 
    this.render('dashboard'); 
} 
+0

感谢这似乎工作。但是使用这个例子我试图订阅多个集合,但是waitOn并不等待它们完成。 – srinivas 2015-02-12 12:09:16

+0

@srinivas waitOn应该等待他们每个人,因为这个原因我放了一个数组运算符'[xx]',把每个'Meteor.subscribe'放在那里。 – Akshat 2015-02-12 12:26:56

+0

我已经完成了,但它不会等待,即使我正在检查该操作块。更新我的问题以证明这一点。 – srinivas 2015-02-12 12:50:04