Accounts.createUser中的Meteor异步调用

时间:2017-09-26 18:14:03

标签: javascript meteor

在注册新用户之前,我需要使用该电子邮件删除之前的用户并查询API以将更多信息填入用户作为产品要求。

不幸的是我无法实现它,这是我从服务器获得的错误:Exception while invoking method 'createUser' Error: insert requires an argument

到目前为止我做的是:

客户端:

Accounts.createUser({ email, password, profile: { something } }, (err) => {
  if (err) {
    console.error(err2.reason);
  }
  history.replace('/account');
});

服务器:

Accounts.onCreateUser((options, user) => {
  Meteor.users.remove({ email: options.email }, () => {
    try {
      const res = request.postSync(authenticate, {
        method: 'POST',
        json: true,
        body: {
          email: options.email,
          password: options.profile.password
        }
      });

      if (res.response.statusCode < 300) {
        const newUser = user;
        newUser.profile = {};
        newUser.profile.user_id = res.body.response.user_id;
        newUser.profile.token = res.body.response.token;
        return newUser;
      }

      throw new Meteor.Error(res.response.body.error.message);
    } catch (err) {
      throw new Meteor.Error(err.message);
    }
  });
});

我做错了什么?感谢

1 个答案:

答案 0 :(得分:0)

来自Accounts.onCreateUser documentation

  

该函数应返回用户文档(传入的文档或新创建的对象)以及所需的任何修改。返回的文档将直接插入<HorizontalScollView android:layout_width="wrap_content" android:layout_height="wrap_content"> <!-- View want to scroll --> </HorizontalScollView> 集合。

你的函数虽然没有返回任何内容,但只是调用Meteor.users并将某个函数作为第二个参数。别忘了Meteor中的DB调用是同步,所以它应该是这样的:

Meteor.users.remove()