Method on the server side is invoked two times, why?

I have problem with method on the server side. It is invoked two times, but it should only one. Error occur when I change password. To change password I use Accounts.setpassword(). Below code of that method:

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;
  }
});

My autoform form and schema to it:

<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>

Schema:

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
  }
});

Shot in the dark - If you have two instances of {{>userSettingsTemplate}} in your html, maybe the autoform hooks are getting set up twice. Source

1 Like