2016-06-12 143 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(),它在客戶端上運行,並且是爲了做你想做的事情。

相關問題