javascript - 如何使用 ian :accounts-ui-bootstrap-3? 根据 Meteor 中的用户输入设置配置文件字段值

标签 javascript meteor twitter-bootstrap-3 meteor-accounts

我正在使用 ian:accounts-ui-bootstrap-3 在我的 Meteor 应用中创建帐户。在该应用程序中,我使用稍微定制的版本,其中用户声明他/她的用户名和性别。

Accounts.ui.config({
    requestPermissions: {},
    extraSignupFields: [{
        fieldName: 'username',
        fieldLabel: 'Username',
        inputType: 'text',
        visible: true,
        validate: function(value, errorFunction) {
            if (!value) {
                errorFunction("Please write your username");
                return false;
            } else {
                return true;
            }
        }
    }, {
        fieldName: 'gender',
        showFieldLabel: false, // If true, fieldLabel will be shown before radio group
        fieldLabel: 'Gender',
        inputType: 'radio',
        radioLayout: 'vertical', // It can be 'inline' or 'vertical'
        data: [{ // Array of radio options, all properties are required
            id: 1, // id suffix of the radio element
            label: 'Male', // label for the radio element
            value: 'm' // value of the radio element, this will be saved.
        }, {
            id: 2,
            label: 'Female',
            value: 'f',
            checked: 'checked'
        }],
        visible: true
    }, ]
});

这工作正常,我得到一个用户,其数据(有些删节)如下所示:

{
    "username" : "testuser",
    "emails" : [
        {
            "address" : "test@test.com",
            "verified" : false
        }
    ],
    "profile" : {
        "username" : "testuser",
        "gender" : "f"
    }
}

现在我想使用“性别”值将名为“avatar”的新字段设置为“generic-male.png”或“generic-female.png”。在帖子https://stackoverflow.com/questions/34435674/meteor-accounts-entry-how-to-set-extrasignupfields-to-meteor-user-profile建议使用账户。 onCreateUser机制。所以我写了这个,并将其放在服务器目录的main.js文件中:

Accounts.onCreateUser(function(user){
  if (user.gender=='m') {
    user.avatar = "generic-male.png"
  } else {
    user.avatar = "generic-female.png"
  }
})

但这会产生以下错误:

Exception while invoking method 'createUser' Error: insert requires an argument

我想这是因为 user 参数没有传递给 onCreateUser 方法。但应该怎样做呢?

最佳答案

您需要在accountsServer.onCreateUser(func)中返回user功能:

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.

例如:

Accounts.onCreateUser(function(user){
  if (user.gender === 'm') user.avatar = "generic-male.png";
  else user.avatar = "generic-female.png";
  return user;
});

关于javascript - 如何使用 ian :accounts-ui-bootstrap-3? 根据 Meteor 中的用户输入设置配置文件字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36603326/

相关文章:

用于验证 URL 的 Javascript 代码在 IE 和 Chrome 中有效,但在 Firefox 中无效

node.js - 两个空格自动转换为 &nbsp (Heroku、Mongodb)

testing - 使用 Laika 和 PhantomJS 进行集成测试

javascript - meteor .js : Error not caught on client in method call

html - 如何模仿谷歌的粗红色下划线文本装饰

jquery - 如何确定 Bootstrap 3 列是否已堆叠

javascript - <audio> 开始播放时发生的事件

javascript - 从文件中提取数据

javascript - 如何从 JavaScript 调整表格列宽

css - Bootstrap 3 - 跨网格将文本对齐到表单控件项