【发布时间】:2016-12-05 16:55:22
【问题描述】:
我正在使用 Angular2 中的 Firebase,但我无法在承诺中访问 this。
这是我的脚本。上下文理解:登录时,我想检查数据库中与该用户相关的基本数据是否仍然是最新的。
import { Injectable, Inject } from '@angular/core';
import { AngularFire, AuthProviders, FirebaseAuthState, FirebaseApp } from 'angularfire2';
@Injectable()
export class AuthService {
user;
userUID;
userEmail;
isAuthenticated: boolean = false;
constructor(public af: AngularFire, @Inject(FirebaseApp) firebaseApp: firebase.app.App) {
this.af.auth.subscribe(authState => {
if (authState && !authState.auth.isAnonymous) {
this.user = authState;
this.isAuthenticated = true;
this.userEmail = this.user.auth.email;
this.userUID = this.user.auth.uid;
firebaseApp.database().ref('/users/' + this.userUID).once('value').then(function(snapshot) {
if (snapshot.val().email !== this.userEmail) { //ERROR HERE
console.log('update the email in database here');
}
});
console.log('User Logged in', this.user, this.userEmail, this.userUID);
return;
}
this.logout();
console.log('User Not Authenticated', this.user);
});
}
错误当然是在我要进行比较的那一行:
if (snapshot.val().email !== this.userEmail)
我知道这在 JavaScript/typescript 中很烦人,我找不到让我的脚本正常工作的方法。
【问题讨论】:
标签: angular typescript firebase firebase-authentication es6-promise