在尝试使用它来更改其他用户的电子邮件之前,如何检查用户是否已存在电子邮件(电子邮件[0] .address)?

时间:2017-01-12 13:12:34

标签: meteor

我有一种更改用户地址的方法(每个用户只有一个地址,所以它是emails[0].address)。

在该方法中,如果另一个用户具有相同的内容,Accounts.addEmail(this.userId, newemail);会严格阻止添加emails[0].address。我在客户端收到了error.reason === 'Email already exists.' Great。

但在致电Accounts.addEmail(this.userId, newemail);之前,我需要致电Accounts.removeEmail(this.userId, emailold);,他们会移除旧地址并emails[0].address免费Accounts.addEmail(this.userId, newemail);(当没有电子邮件地址时)在帐户中,默认使用emails[0].address)。

那么,如果Accounts.removeEmail(this.userId, emailold);被其他任何用户newemail用作emails[0].address,我如何处理并停止// Change Email Address of the User Meteor.methods({ addNewEmail: function(emailold, newemail) { // this function is executed in strict mode 'use strict'; // Consistency var check check([emailold, newemail], [String]); // Let other method calls from the same client start running, // without waiting this one to complete. this.unblock(); //Remove the old email address for the user (only one by default) Accounts.removeEmail(this.userId, emailold); //Add the new email address for the user: by default, setted to verified:false Accounts.addEmail(this.userId, newemail); // Send email verification to the new email address Accounts.sendVerificationEmail(this.userId, newemail); return true; } });

在我的方法之下。

由于

{{1}}

1 个答案:

答案 0 :(得分:1)

您可以直接更新users集合,并自行处理任何错误。这就是我的工作:

Meteor.methods({
  "users.changeEmail"(address) {
    check(address, String);

    const existingAddressCheck = Meteor.users.findOne({"emails.0.address": address});

    if(existingAddressCheck) {
      if(existingAddressCheck._id === Meteor.userId()) {
        throw new Meteor.Error("users.changeEmail.sameEmail", "That's already your registered email address!");
      } else {
        throw new Meteor.Error("users.changeEmail.existing", "An account with that address already exists");
      }
    }
    return Meteor.users.update({_id: Meteor.userId()}, {$set: {"emails.0": {address, verified: false}}});
  }
});