Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Firebase Angular2 promise中访问此文件_Angular_Typescript_Firebase_Firebase Authentication_Es6 Promise - Fatal编程技术网

在Firebase Angular2 promise中访问此文件

在Firebase Angular2 promise中访问此文件,angular,typescript,firebase,firebase-authentication,es6-promise,Angular,Typescript,Firebase,Firebase Authentication,Es6 Promise,我在Angular2玩Firebase,我被困在访问这个的承诺中 这是我的剧本。对于上下文理解:在登录时,我想检查数据库中与该用户相关的基本数据是否仍然是最新的 import { Injectable, Inject } from '@angular/core'; import { AngularFire, AuthProviders, FirebaseAuthState, FirebaseApp } from 'angularfire2'; @Injectable() export clas

我在Angular2玩Firebase,我被困在访问
这个
的承诺中

这是我的剧本。对于上下文理解:在登录时,我想检查数据库中与该用户相关的基本数据是否仍然是最新的

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中的这一点非常烦人,我找不到让我的脚本工作的方法

如果使用箭头函数,它将保留此
所指的内容

firebaseApp.database().ref('/users/' + this.userUID).once('value').then((snapshot) => {
    if (snapshot.val().email !== this.userEmail) {
        console.log('update the email in database here');
    }
});

如果使用箭头函数,它将保留此
所指的内容

firebaseApp.database().ref('/users/' + this.userUID).once('value').then((snapshot) => {
    if (snapshot.val().email !== this.userEmail) {
        console.log('update the email in database here');
    }
});

解决这个问题的JavaScript标准技巧是事先将
this
保存到变量中,然后使用它,通常是
让self=this
。 在TypeScript中,当您使用fat arrow函数时,该语言会为您执行此操作。编译后的JavaScript代码将有一个常规函数,但会在函数内部使用一个名为
\u this
的变量,该变量预先初始化为
this
,而不是
this

简而言之,使用
(param)=>{//code}
而不是编写
函数(param){//code}
解决这个问题的JavaScript标准技巧是事先将
这个
保存到变量中,然后使用它,通常是
让self=this
。 在TypeScript中,当您使用fat arrow函数时,该语言会为您执行此操作。编译后的JavaScript代码将有一个常规函数,但会在函数内部使用一个名为
\u this
的变量,该变量预先初始化为
this
,而不是
this

简而言之,不要编写
函数(param){//code}
,而是使用
(param)=>{//code}

谢谢,它可以按预期工作。。。必须提高我的打字技能。谢谢。它的工作如预期。。。我必须学习打字技巧。