Search code examples
angular2-meteor

angular2-meteor changePassword


My code is following:

password-change.ts

import { Component, OnInit } from '@angular/core';
import { REACTIVE_FORM_DIRECTIVES, FormGroup, FormBuilder, Validators } from '@angular/forms';
import { Meteor } from 'meteor/meteor';
import { MeteorComponent } from 'angular2-meteor';
import { Router , ROUTER_DIRECTIVES} from '@angular/router';
import { MdCheckbox } from '@angular2-material/checkbox';
import { MD_INPUT_DIRECTIVES } from '@angular2-material/input';
import { MD_BUTTON_DIRECTIVES } from '@angular2-material/button';
import { Accounts } from 'meteor/accounts-base';
import { MD_TOOLBAR_DIRECTIVES } from '@angular2-material/toolbar';

import template from './password-change.component.html';

@Component({
  selector: 'password-change',
  template,
  directives: [REACTIVE_FORM_DIRECTIVES, MdCheckbox, MD_INPUT_DIRECTIVES, MD_BUTTON_DIRECTIVES,MD_TOOLBAR_DIRECTIVES]
})
export class PasswordChangeComponent extends MeteorComponent implements OnInit {
  passwordForm: FormGroup;
  error: string;

  constructor(
    private router: Router,
    private formBuilder: FormBuilder
    ) {
      super();
  }

  ngOnInit() {
    this.passwordForm = this.formBuilder.group({
      oldPassword: ['',Validators.required],
      newPassword: ['', Validators.required],
    });

     this.error = '';
  }

  ChangePassword() {
    if (this.passwordForm.valid) {
      Accounts.changePassword({oldPassword:this.passwordForm.value.oldPassword,newPassowrd:this.passwordForm.value.newPassword,}
      ), (err) => {
        if (err) {
          this.error = err;
        } else {
          this.router.navigate(['/party']);
        }
      };
    }
  }
}

There are an errors saying that the supplied parameter doesn't match with the call target. The are asking oldPassword:string, not the typing is oldPassword:any

How can i fix it?


Solution

  • You may try to store the oldPassword in a typed string variable like this :

    let tmpOldPassword:string = this.passwordForm.value.oldPassword

    And then passed it to the Accounts.changePassword function :

    if (this.passwordForm.valid) {   
         let tmpOldPassword:string = this.passwordForm.value.oldPassword;
         Accounts.changePassword({oldPassword:tmpOldPassword, ...}
         //...
    }