【发布时间】:2018-03-01 23:53:43
【问题描述】:
我需要这样做:
async doLogin(user: User) {
try {
const result = await this.AngularFireAuth.auth.signInWithEmailAndPassword(user.email, user.password);
if (result) {
this.AngularFireAuth.authState.subscribe(user => {
if (user) {
var ref = firebase.database().ref(`profile/${user.uid}`);
ref.once("value")
.then(function(snapshot) {
var key = snapshot.key;
if (user.uid === key) {
this.navCtrl.setRoot(HomePage);
}
else {
this.navCtrl.setRoot(ProfilePage);
}
});
}
});
}
}
catch (error) {
let toast = this.toastCtrl.create({
message: this.loginErrorString,
duration: 3000,
position: 'top'
});
toast.present();
}
}
但我收到了这个错误:TypeError: Cannot read property 'navCtrl' of undefined
我不知道为什么会这样,但我认为 doLogin 方法无法读取模块 navCtrl。明显增加了navCtrl方法构造。
最好的问候朋友
【问题讨论】:
-
把
.then(function(snapshot) {改成箭头函数。function关键字创建一个新的this上下文
标签: angular typescript firebase firebase-realtime-database ionic3