gpt4 book ai didi

javascript - Meteor.users 上的自定义字段未发布

转载 作者:可可西里 更新时间:2023-11-01 02:14:13 26 4
gpt4 key购买 nike

我的最终目标是让 CUSTOM_FIELD_I_FREAKEN_WANT_TO_PUBLISH 在模板登录后通过 {{currentUser}} 可用,但是 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()),在通过模板访问时也未定义。

这是一个错误吗?我做错了什么吗?

最佳答案

参见 this answer:


By default the server publishes username, emails, and profile

因此您需要发布/订阅额外的字段。

服务器:

Meteor.publish('userData', function() {
if(!this.userId) return null;
return Meteor.users.find(this.userId, {fields: {
permission: 1,
}});
});

客户:

Deps.autorun(function(){
Meteor.subscribe('userData');
});


查看您的代码,缺少的部分是订阅时自动运行。如果没有它,订阅会在应用程序加载时调用一次,并且不会在用户更改时更改 - 例如,当他第一次设置时。

关于javascript - Meteor.users 上的自定义字段未发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19391308/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com