几个月前,我写了这个Fire Auth函数,但是今天尝试时,它返回了以下错误:
错误TypeError:无法读取null的属性'uid'
我似乎无法弄清楚哪里出了问题,因为我没有更改任何代码。
根据Firebase控制台,用户已成功登录。
代码:
login.ts,后跟login.html
import { Component } from '@angular/core';
import { IonicPage, NavController, ToastController, NavParams } from 'ionic-angular';
//Firebase imports
import { AngularFireAuth } from 'angularfire2/auth';
//Model import
import { User } from '../../models/user.interface';
//Page import
import { HomePage } from '../home/home';
@IonicPage()
@Component({
selector: 'page-login',
templateUrl: 'login.html',
})
export class LoginPage {
oldUser = {} as User;
constructor(
private afAuth: AngularFireAuth,
public navCtrl: NavController,
public navParams: NavParams,
private toast: ToastController) {
}
async login(oldUser: User) {
try {
const result = this.afAuth.auth.signInWithEmailAndPassword(oldUser.email, oldUser.password);
if (result) {
this.navCtrl.setRoot(HomePage);
this.toast.create({
message: `We're all given the same hours in a day. Are you ready to maximise yours?`,
duration: 3000
}).present();
}
}
catch(e) {
this.toast.create({
message: `Let's try again: ` + `${e}`.substr(7),
duration: 2000
}).present();
}
}
}
<div class="list">
<ion-item>
<ion-input class="title" type="email" placeholder="Email Address" [(ngModel)]="oldUser.email"></ion-input>
</ion-item>
<ion-item>
<ion-input class="title" placeholder="Password" clear-input type="password" [(ngModel)]="oldUser.password"></ion-input>
</ion-item>
</div>
<button ion-button (click)="login(oldUser)">LOGIN</button>
请帮助!!谢谢!
答案 0 :(得分:0)
u需要检查用户是否存在1,然后执行yourlogic
let user = this.fireAuth.auth.currentUser;
if (user != null) {
currentUser.id = user.uid;
currentUser.email = user.email;
// login logic ...
}
更多信息here