Ionic v4 Firebase:无法读取未定义的 属性 'email'

Ionic v4 Firebase: cannot read property 'email' of undefined

这段代码已经困扰我四个小时了...它应该使用 Firebase 的身份验证系统和 Ionic 4 组件注册用户。

取而代之的是 return 大约 6 次:

ERROR TypeError: Cannot read property 'email' of undefined

另一个奇怪的事情是我的代码只会挑剔 属性 'email',而不会 return 错误 属性 'password'.

signup.page.ts

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';

// firebase imports
import * as firebase from 'firebase';
import { AngularFireAuth } from '@angular/fire/auth';
import { ToastController } from '@ionic/angular';

// model import 
import { User } from '../../models/login.interface';

@Component({
  selector: 'app-signup',
  templateUrl: './signup.page.html',
  styleUrls: ['./signup.page.scss'],
})
export class SignupPage implements OnInit {

  constructor(
    private afAuth: AngularFireAuth,
    private route: Router,
    public toast: ToastController
  ) { }

  ngOnInit() {
  }

  async signup(user: User) {
    console.log('starting auth');
    try {
      const result = await firebase.auth().createUserWithEmailAndPassword(user.email, user.password);
      if (result) {
          this.route.navigateByUrl('/add-profile');
      }
    } catch (e) {
      console.log(e);
    }
  }

}

signup.page.html

<ion-header>
  <ion-toolbar>
    <ion-title>SIGNUP</ion-title>
  </ion-toolbar>
</ion-header>

<ion-content padding>
  <ion-item>
    <ion-label position="stacked">Email</ion-label>
    <ion-input type="text" [(ngModel)]="user.email"></ion-input>
  </ion-item>
  <ion-item>
    <ion-label position="stacked">Password (6+ characters)</ion-label>
    <ion-input type="password" [(ngModel)]="user.password"></ion-input>
  </ion-item>
  <ion-fab vertical="bottom" horizontal="end" slot="fixed">
    <ion-fab-button (click)="signup(user)">SIGNUP
    </ion-fab-button>
  </ion-fab>
</ion-content>

user.interface.ts

export interface User {
  email: string;
  pasword: string;
}

如有任何帮助,我们将不胜感激!谢谢!

创建一个用户并在构造函数或 ngOnInit 中用空值初始化它,如下所示

用户:用户;

//内部构造函数或ngOnInit this.user = { 电子邮件: '', 密码:'' }

// firebase imports
import * as firebase from 'firebase';
import { AngularFireAuth } from '@angular/fire/auth';
import { ToastController } from '@ionic/angular';

// model import 
import { User } from '../../models/login.interface';

@Component({
  selector: 'app-signup',
  templateUrl: './signup.page.html',
  styleUrls: ['./signup.page.scss'],
})
export class SignupPage implements OnInit {

  user: User = {
  email: '',
  password: ''  
  }
  constructor(
    private afAuth: AngularFireAuth,
    private route: Router,
    public toast: ToastController
  ) { }

  ngOnInit() {
  }

  async signup(user: User) {
    console.log('starting auth');
    try {
      const result = await firebase.auth().createUserWithEmailAndPassword(user.email, user.password);
      if (result) {
          this.route.navigateByUrl('/add-profile');
      }
    } catch (e) {
      console.log(e);
    }
  }

}