Meteor.users 上的自定义字段未发布 [英] Custom fields on Meteor.users not being published
问题描述
我的最终目标是让 CUSTOM_FIELD_I_FREAKEN_WANT_TO_PUBLISH
通过 {{currentUser}}
可用于模板(如果它们已登录),但 Meteor 不会从用户集合.
My end goal is to have CUSTOM_FIELD_I_FREAKEN_WANT_TO_PUBLISH
available to templates via {{currentUser}}
if they are logged in, but Meteor is not sending down all the fields from the users collection.
在服务器中:
Meteor.publish('userdata', function() {
this.ready(); // do I really even need to do this?
//return Meteor.users.find(); //This STILL doesn't work
return Meteor.users.findOne({_id: this.userId});
});
在客户端:
var s = Meteor.subscribe('userdata', function() {
console.log('userdata available');
console.log('s.ready: '+s.ready())
});
console.log('s.ready: '+s.ready())
我可以通过直接连接到 mongo 实例并键入:db.users.find()
:
I can verify that there are fields in the collection by connecting to the mongo instance directly and typing: db.users.find()
:
{
"_id" : "N2M7Zp265vkbTBTFF",
"createdAt" : ISODate("2013-10-15T03:29:53.155Z"),
"CUSTOM_FIELD_I_FREAKEN_WANT_TO_PUBLISH" : "P4GRrQMixEZducmuo",
"profile" : {
"name" : "Jonathan Dumaine",
...,
},
"services" : {
"facebook" : {
....,
}
}
}
在客户端验证订阅准备就绪后,用户集合中仅有的字段是 _id
和 _profile
.附加字段在客户端中不可见(通过控制台 Meteor.users.find().fetch()
),也不会在通过模板访问时定义.
After verifying that the subscription is ready in the client, the only fields on the users collection are _id
and _profile
. The additional fields are not visible in the client (via console Meteor.users.find().fetch()
) nor are they defined when accessed through templates.
这是一个错误吗?我做错了什么吗?
Is this a bug? Am I doing something wrong?
推荐答案
默认情况下,服务器发布用户名、电子邮件和个人资料
By default the server publishes username, emails, and profile
因此您需要发布/订阅附加字段.
So you need to publish / subscribe for the additional fields.
服务器:
Meteor.publish('userData', function() {
if(!this.userId) return null;
return Meteor.users.find(this.userId, {fields: {
permission: 1,
}});
});
客户:
Deps.autorun(function(){
Meteor.subscribe('userData');
});
查看您的代码,缺少的部分是订阅时自动运行的.如果没有它,订阅将在应用加载时调用一次,并且在用户更改时不会更改 - 例如,当他第一次设置时.
Looking at your code, the missing part is autorun on subscription. Without it, the subscription is called once when the app is loaded and does not change when the user changes - when he is set for the first time, for example.
这篇关于Meteor.users 上的自定义字段未发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!