简体   繁体   English

流星:尝试向用户配置文件Accounts.onCreateUser()添加另一个字段;

[英]Meteor: trying to add another field to the user profile Accounts.onCreateUser();

Basically trying to modify the user that was just created by giving it an extra field called sid in it's profile object. 基本上是尝试通过在配置文件对象中为其添加一个名为sid的额外字段来修改刚刚创建的用户。 I'm running this on server.js (the server code) 我在server.js(服务器代码)上运行

Accounts.onCreateUser(function (options, user) {      
        Meteor.users.update({_id: user._id}, {$set: {"user.profile.sid": [post.content]}});
});
    console.log(JSON.stringify(user));

However, the user object does not show the sid field in it's output. 但是,用户对象的输出中未显示sid字段。 Am I doing this in the wrong location or is my code wrong? 我在错误的位置执行此操作还是我的代码错误?

Try this instead 试试这个

Accounts.onCreateUser(function (options, user) {      
    user.profile.sid = [post.content];
    return user;
});

From the documentation it reads ( http://docs.meteor.com/#/full/accounts_oncreateuser ): 从文档中读取的内容( http://docs.meteor.com/#/full/accounts_oncreateuser ):

The user argument is created on the server and contains a proposed user object... 用户参数在服务器上创建,并包含一个建议的用户对象...

So at this point it looks like the user does not actually exist in the database yet. 因此,在这一点上,用户似乎实际上尚未存在于数据库中。

From the docs 来自文档

The function you pass will be called with two arguments: options and user. 您传递的函数将使用两个参数调用:options和user。 The options argument comes from Accounts.createUser for password-based users or from an external service login flow. options参数来自基于密码用户的Accounts.createUser或来自外部服务登录流。 options may come from an untrusted client so make sure to validate any values you read from it. 选项可能来自不受信任的客户端,因此请确保验证从其中读取的所有值。 The user argument is created on the server and contains a proposed user object with all the automatically generated fields required for the user to log in, including the _id. user参数在服务器上创建,包含一个建议的用户对象,其中包含用户登录所需的所有自动生成的字段,包括_id。

The function should return the user document (either the one passed in or a newly-created object) with whatever modifications are desired. 该函数应返回用户文档(无论是传入的文档还是新创建的对象),并且需要进行任何修改。 The returned document is inserted directly into the Meteor.users collection. 返回的文档将直接插入Meteor.users集合中。

So your code should be: 因此,您的代码应为:

Accounts.onCreateUser(function (options, user) {      
    user.profile.sid = [post.content];
    return user;
});

However be aware that anything in the user.profile object can be changed by your users . 但是请注意,user.profile对象中的任何内容都可以由用户更改

profile: an Object which the user can create and update with any data. 配置文件:用户可以创建并使用任何数据更新的对象。 Do not store anything on profile that you wouldn't want the user to edit unless you have a deny rule on the Meteor.users collection. 除非您对Meteor.users集合具有拒绝规则,否则不要在用户不希望编辑的配置文件上存储任何内容。

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

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