这段代码已经困扰了我四个小时……应该使用Firebase的身份验证系统和Ionic 4组件来注册用户。
相反,它会在 6 次左右返回以下内容:
错误TypeError:无法读取未定义的属性“电子邮件”
另一个奇怪的事情是我的代码只会对属性“ email”进行挑剔,而不会因为具有属性“ 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;
}
任何帮助将不胜感激!谢谢!
答案 0 :(得分:1)
创建用户并在构造函数或ngOnInit中使用空值对其进行初始化,如下所示
user:User;
//内部构造函数或ngOnInit this.user = { 电子邮件:”, 密码:'' }
答案 1 :(得分:0)
// 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);
}
}
}