服务器端的方法被调用两次,为什么?

时间:2016-06-12 14:00:00

标签: javascript meteor

我在服务器端的方法有问题。它被调用两次,但它应该只有一次。更改密码时出错。要更改密码,我使用Accounts.setpassword()。下面是该方法的代码:

import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';
import { TAPi18n } from 'meteor/tap:i18n';
import { Accounts } from 'meteor/accounts-base';
import _ from 'lodash';

Meteor.methods({
  'users/change-user-settings'(formData) {
    if (!this.userId) {
      throw new Meteor.Error(401, TAPi18n.__('errors.you_are_not_logged'));
    }
    check(formData, App.Schemas.userSettingsSchema);

    //change password
    if (formData.password) {
      Accounts.setPassword(this.userId, formData.password, {logout: false});
    }


    //change email
    if (formData.email) {
      Meteor.users.update({_id: this.userId}, {
          $set: {
              'emails.0.address': formData.email
          }
      });
    }

    //change phone number
    if (formData.phoneNumber) {
      Meteor.users.update({_id: this.userId}, {
          $set: {
              'phoneNumber': formData.phoneNumber
          }
      });
    }

    return true;
  }
});

我的autoform形式和架构:

<template name="userSettingsTemplate">
  <div class="user-settings-form-wrapper">
    <h4>Change settings</h4>

   {{ #autoForm id="userSettingsForm" type="method" meteormethod="users/change-user-settings" schema=getUserSettingsSchema class="form-horizontal"}}
      {{ > afQuickField name="password" label="Change password: " template="bootstrap3-horizontal" label-class="col-xs-6" input-col-class="col-xs-6"}}
      {{ > afQuickField name="passwordConfirmation" label="Confirm password: " template="bootstrap3-horizontal" label-class="col-xs-6" input-col-class="col-xs-6"}}
      {{ > afQuickField name="email" label="E-mail: " template="bootstrap3-horizontal" label-class="col-xs-6" input-col-class="col-xs-6"}}
      {{ > afQuickField name="phoneNumber" label="Phone number: " template="bootstrap3-horizontal" label-class="col-xs-6" input-col-class="col-xs-6"}}
      <div class="form-group">
        <button type="submit" class="btn btn-primary full-width">Send</button>
      </div>
    {{ / autoForm }} 

  </div>
</template>

架构:

import { SimpleSchema } from 'meteor/aldeed:simple-schema';
import { TAPi18n } from 'meteor/tap:i18n';
import { Meteor } from 'meteor/meteor';

Meteor.startup(() => {
  // for running tests this is temporary workaround
  try {
    SimpleSchema.messages({
      "passwordMismatch": "passwords are not the same"
    });
  }
  catch (e) {
    console.log(e.message, e.name, e.stack);
  }
});

App.Schemas.userSettingsSchema = new SimpleSchema({
  password: {
    type: String,
    optional: true,
    min: 6,
    autoform: {
      type: "password"
    }
  },
  passwordConfirmation: {
    type: String,
    min: 6,
    optional: true,
    autoform: {
      type: "password"
    },
    custom: function() {
      if (this.value !== this.field('password').value) {
        return "passwordMismatch";
      }
    }
  },
  email: {
    type: String,
    optional: true,
    regEx: SimpleSchema.RegEx.Email
  },
  phoneNumber: {
    type: String,
    optional: true
  }
});

1 个答案:

答案 0 :(得分:-1)

只是旁注。除非我在这里遗漏了一些东西,否则你不应该像这样使用Accounts.setPassword()。它是服务器端方法,因此新密码将通过网络以纯文本形式发送。相反,看看Accounts.changePassword(),它在客户端上运行,并且打算做你想做的事。