[英]Custom fields on Meteor.users not being published
我的最终目标是,如果他们已登录,则可通过{{currentUser}}
将CUSTOM_FIELD_I_FREAKEN_WANT_TO_PUBLISH
提供给模板,但Meteor不会从用户集合中发送所有字段。
在服务器中:
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()
来验证集合中是否有字段:
{
"_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()
),也不会在通过模板访问时定义。
这是一个错误吗? 难道我做错了什么?
看到这个答案:
默认情况下,服务器会发布用户名,电子邮件和个人资料
因此,您需要发布/订阅其他字段。
服务器:
Meteor.publish('userData', function() {
if(!this.userId) return null;
return Meteor.users.find(this.userId, {fields: {
permission: 1,
}});
});
客户:
Deps.autorun(function(){
Meteor.subscribe('userData');
});
查看您的代码,缺少的部分是在订阅时自动运行。 没有它,订阅在加载应用程序时调用一次,并且在用户更改时不会更改 - 例如,当他第一次设置时。
来自文档 :
默认情况下,当前用户的用户名,电子邮件和个人资料将发布到客户端。
因此,为了发布您的字段,您需要执行以下操作:
Meteor.publish('userdata', function() {
return Meteor.users.find({}, {fields: {'CUSTOM_FIELD_I_FREAKEN_WANT_TO_PUBLISH':1}});
});
你非常接近。 你有两个问题:
1)发布中的ready()调用是不必要的,因为如果返回游标则会自动发送。 当您确实需要它时,您希望在发送所有数据后在发布结束时调用它。
2)你想要返回一个用find()
而不是findOne()
完成的游标。
所以你的发布将是:
//on server
Meteor.publish( 'userData', function(){
return Meteor.users.find({_id: this.userId});
});
您显示的代码实际上并不测试是否收到任何数据,以确保不是问题尝试:
//on client
var s = Meteor.subscribe('userdata', function() {
console.log('userdata available');
console.log('s.ready: '+s.ready());
console.log('userData: ' + JSON.stringify( Meteor.users.findOne()) );
});
console.log('s.ready: '+s.ready())
此外,正如David Weldon在他的回答中所说,您可能希望通过在您发布的find()中设置fields选项来指定和限制哪些字段被发送到客户端。
我想,您可能正在尝试从文档http://docs.meteor.com/#accounts_oncreateuser中找到函数Accounts.onCreateUser()
。 有一个确切的例子,如何将任何对象添加到用户文档,旁边的个人资料,电子邮件,_id等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.