2016-06-12 121 views
-2

我在服务器端的方法有问题。它被调用两次,但它应该只有一个。当我更改密码时发生错误。要更改密码,我使用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; 
    } 
}); 

我自动窗体形式和模式,以它:

<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

只是一个方面说明。除非我在这里错过了一些东西,否则你不应该这样使用Accounts.setPassword()。这是一种服务器端方法,因此新密码将以纯文本形式通过线路发送。相反,请看Accounts.changePassword(),它在客户端上运行,并且是为了做你想做的事情。

相关问题