我在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');
},
你可以发表你的发布方法吗? =) – ahren 2015-02-12 06:20:46
@ahren添加了发布方法 – srinivas 2015-02-12 06:33:34