0

这是我之前发送的问题的分支Retrieve value from html 'value' attribute。我现在尝试在创建用户时将会话变量值插入名为“userType”的字段中。我保留了不安全的包,所以我可以立即执行 Meteor.users.find().count(); 在控制台中。到目前为止,尚未创建用户。

我是否以正确的方式插入会话变量值,是否应该使用 Accounts.onCreateUser 在服务器端插入此会话值?

客户端js

Template.joinForm.events({
'submit .form-join': function(e, t) {
    e.preventDefault();
    var firstName = t.find('#firstName').value,
    lastName = t.find('#email').value,
    email = t.find('#email').value,
    password = t.find('#password').value,
    username = firstName + '.' + lastName,
    profile = {
            name: firstName + ' ' + lastName,
            userType: selectedUserType
};

    Accounts.createUser({
        email: email,
        username: username,
        password: password,
        profile: profile
    }, function(error) {
        if (error) {
            alert(error);
        } else {
            Router.go('/');
        }
    });
}
});

我已将“userType”会话变量设为全局,请参见如下...

Template.authJoinType.events({
'click div.join-type-inner': function(e, tmpl) {
    userType = $(e.target).attr("value");
    Session.set('userType', userType);
    selectedUserType = Session.get('userType');
    console.log(selectedUserType);
}
});
4

1 回答 1

1

createUser接受一个选项对象,最多包含四个字段:usernameemailpasswordprofile。您正在传递被忽略的第五个字段。为了将userType数据传输到服务器,您需要将其添加到profile调用中的对象中createUser


如果userType存在于用户文档的根目录(而不是配置文件中)很重要,您可以在onCreateUser回调中修改它,如下所示:

客户

profile = {
  name: firstName + ' ' + lastName,
  userType: userType
};

服务器

Accounts.onCreateUser(function(options, user) {
  if (options.profile) {
    if (options.profile.userType) {
      user.userType = options.profile.userType;
      delete options.profile.userType;
    }
    user.profile = options.profile;
  }

  return user;
});

Meteor.publish(null, function() {
  // automatically publish the userType for the connected user
  // no subscription is necessary
  return Meteor.users.find(this.userId, {fields: {userType: 1}});
});
于 2014-12-03T15:50:17.060 回答