createUserWithEmailAndPassword error whit Reactive表单

时间:2018-05-26 14:31:09

标签: javascript angular firebase firebase-authentication

之前我使用过ngModel,一切都很好。现在我使用Reactive表单在Firebase中创建用户。

我的问题是如何访问每个字段的值,因为它标记了以下错误:createUserWithEmailAndPassword failed: First argument" email "must be a valid string

component.html

<form [formGroup]="forma" (ngSubmit)="nuevoUsuario()" novalidate>        
   <mat-form-field appearance="outline">
    <mat-label>E-mail</mat-label>
    <input type="email" matInput placeholder="Ingresa tu E-mail" formControlName="email">
  </mat-form-field>
  <mat-form-field appearance="outline">
    <mat-label>Contraseña</mat-label>
    <input type="password" matInput placeholder="Genera tu contraseña" formControlName="password">
  </mat-form-field>
  <button type="submit" class="btn btn-lg btn-primary btn-block mt-3" [disabled]="!forma.valid" >Crear cuenta <i class="fas fa-user-plus ml-2"></i></button>
</form>

component.ts

email: any;
password: string;

constructor(private fb: FormBuilder, public router: Router, public authService: FirebaseService) {  
this.forma = fb.group ({
  email: [ 'xxx@xxx.com', [Validators.required, Validators.email] ],
  password: [ 'xxx', Validators.required ],
})
}

nuevoUsuario() {
firebase.auth().createUserWithEmailAndPassword(this.email, this.password)
  .then(userData => {
    userData.sendEmailVerification();
    this.router.navigate(['/ingresar']);
    console.log(userData)
  })
  .catch(err => {
    console.log(err)
  });
}

1 个答案:

答案 0 :(得分:0)

我通过以这种方式访问​​解决了它:

firebase.auth().createUserWithEmailAndPassword(this.forma.value.email, this.forma.value.password)