问题描述
我的最终目标是通过 {{currentUser}}
向模板提供 CUSTOM_FIELD_I_FREAKEN_WANT_TO_PUBLISH
,但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?
推荐答案
参见
默认情况下,服务器发布用户名,电子邮件和个人资料
所以你需要发布/订阅对于其他字段。
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上的自定义字段未发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!