简体   繁体   English

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

[英]Custom fields on Meteor.users not being published

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. 我的最终目标是,如果他们已登录,则可通过{{currentUser}}CUSTOM_FIELD_I_FREAKEN_WANT_TO_PUBLISH提供给模板,但Meteor不会从用户集合中发送所有字段。

In Server: 在服务器中:

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});
});

In Client: 在客户中:

var s = Meteor.subscribe('userdata', function() { 
    console.log('userdata available');
    console.log('s.ready: '+s.ready())
});
console.log('s.ready: '+s.ready())

I can verify that there are fields in the collection by connecting to the mongo instance directly and typing: db.users.find() : 我可以通过直接连接到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" : {
            ....,
        }
    }
}

After verifying that the subscription is ready in the client, the only fields on the users collection are _id and _profile . 在验证客户端中的订阅准备就绪后,用户集合上的唯一字段是_id_profile The additional fields are not visible in the client (via console Meteor.users.find().fetch() ) nor are they defined when accessed through templates. 其他字段在客户端中不可见(通过控制台Meteor.users.find().fetch() ),也不会在通过模板访问时定义。

Is this a bug? 这是一个错误吗? Am I doing something wrong? 难道我做错了什么?

See this answer: 看到这个答案:


By default the server publishes username, emails, and profile 默认情况下,服务器会发布用户名,电子邮件和个人资料

So you need to publish / subscribe for the additional fields. 因此,您需要发布/订阅其他字段。

Server: 服务器:

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

Client: 客户:

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. 没有它,订阅在加载应用程序时调用一次,并且在用户更改时不会更改 - 例如,当他第一次设置时。

From the docs : 来自文档

By default, the current user's username, emails and profile are published to the client. 默认情况下,当前用户的用户名,电子邮件和个人资料将发布到客户端。

So in order to publish your field, you will need to do something like: 因此,为了发布您的字段,您需要执行以下操作:

Meteor.publish('userdata', function() {
  return Meteor.users.find({}, {fields: {'CUSTOM_FIELD_I_FREAKEN_WANT_TO_PUBLISH':1}});
});

You were very close. 你非常接近。 You have two problems: 你有两个问题:

1) The ready() call in publish is unnecessary since it is sent automatically if you return a cursor. 1)发布中的ready()调用是不必要的,因为如果返回游标则会自动发送。 When you do need it you want to call it at the end of the publish after all data is sent. 当您确实需要它时,您希望在发送所有数据后在发布结束时调用它。

2) You want to return a cursor which is done with find() instead of findOne() . 2)你想要返回一个用find()而不是findOne()完成的游标。

So your publish would be: 所以你的发布将是:

//on server
Meteor.publish( 'userData', function(){
  return Meteor.users.find({_id: this.userId});
});

Your code shown does not actually test if any data has been received so to make sure that is not the problem try: 您显示的代码实际上并不测试是否收到任何数据,以确保不是问题尝试:

//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())

Also, as David Weldon says in his answer, you would probably want to specify and limit exactly what fields get sent to the client by setting a fields option in the find() you publish. 此外,正如David Weldon在他的回答中所说,您可能希望通过在您发布的find()中设置fields选项来指定和限制哪些字段被发送到客户端。

I think, you were probably trying to find function Accounts.onCreateUser() from docs http://docs.meteor.com/#accounts_oncreateuser . 我想,您可能正在尝试从文档http://docs.meteor.com/#accounts_oncreateuser中找到函数Accounts.onCreateUser() There is exact example how to add any object into user doc, next to profile, email, _id, etc. 有一个确切的例子,如何将任何对象添加到用户文档,旁边的个人资料,电子邮件,_id等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM