Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.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
Angular 为什么这个函数不返回任何值?_Angular_Typescript_Firebase_Firebase Authentication - Fatal编程技术网

Angular 为什么这个函数不返回任何值?

Angular 为什么这个函数不返回任何值?,angular,typescript,firebase,firebase-authentication,Angular,Typescript,Firebase,Firebase Authentication,我是个新手。我正在尝试为检查用户是否登录创建一个函数,但onAuthStateChanged中的函数不返回任何值 async loginStatus() { firebase.auth().onAuthStateChanged(firebaseUser => { if ( firebaseUser) { console.log(firebaseUser.uid) this.isuser = true } else {

我是个新手。我正在尝试为检查用户是否登录创建一个函数,但onAuthStateChanged中的函数不返回任何值

 async loginStatus() { 
     firebase.auth().onAuthStateChanged(firebaseUser => {
      if ( firebaseUser) {
       console.log(firebaseUser.uid)
        this.isuser = true
    } else {
       this.isuser = false
    }
  })

return  this.isuser ;
}
打印登录状态未定义

登录状态函数不会返回任何内容。return语句位于onAuthStateChanged回调中。您将返回一个赋值表达式,它将始终为真

根据这里的firebase文档

firebase.auth不是异步函数。你不必等待它

为了实现有意义的回调,如

const userObject=等待登录状态 您需要在loginStatus函数中返回一个承诺,如下所示

异步登录状态{ 返回新PromiseSolve=>{ firebase.auth.OnAuthStateChangeDfreBaseUser=>resolvefirebaseUser } }
希望这有帮助。

如果您只需检查一次是否有firebase用户,您可以按照Frank van Puffelen的建议返回此.currentUser。无需承诺:

loginStatus(): boolean {
  return !!firebase.auth().currentUser;
}
用法:

checkLoginStatus(): void {
  this.isuser = this.loginStatus();
}
但是,如果您希望侦听用户更改,则需要在附加侦听器的方法内执行以下逻辑:

loginStatus(): void {
  firebase.auth().onAuthStateChanged((user) => this.isuser = !!user);
}
使用此方法,该方法不会返回任何内容

你也可以考虑使用可观测数据,但是你也可以看看@angular/fire。一个简单的未经测试的可观察对象如下所示:

readonly loginStatus$: Observable<boolean> = new Observable((subscriber) =>
  firebase.auth().onAuthStateChanged((user) => subscriber.next(!!user))
).pipe(
  distinctUntilChanged(),
  startWith(!!firebase.auth().currentUser),
  shareReplay(1)
);
作为补充说明,通过声明类型,您可以告诉typescript编译器您希望该方法返回什么。如果它不符合要求,它将给您一个描述错误。调试东西要容易得多:

如果您想知道用户的当前登录状态,无需附加侦听器。您只需读取firebase.auth.currentUser:


如果您希望在用户登录或注销时执行特定操作,则需要一个身份验证状态侦听器。但是在这种情况下,您不能将用户的状态返回给调用方,执行操作的代码应该在auth state侦听器中。

为什么不直接解析!!firebaseUser而不是if/else?auth.onAuthStateChangedFereBaseUser=>resolve!!firebaseUser@MatthewLayton我同意你的观点,解析用户对象将是一个更好的代码结构。但我不确定他在这里的意图。我只是尽可能地重用他的代码结构。他可能想在if/else语句中添加一些逻辑。谢谢,但返回object auth[object Promise]how exit value而不使用this函数。checkLoginStatus.thendata=>console.logdata,因为无法使用value out函数@yueyou这个函数现在只返回Promise,因为我试图使它与您原来的函数post类似。如果要在此函数之外使用用户对象。像resolvefirebaseUser一样将对象置于resolve中。然后这个函数将返回一个Promise+1谢谢,但返回相同的值[object Promise],我不关心值,我关心类型,需要返回值才能使用guard。我几乎要依赖你的回答了@YUEYOU此代码与简单返回firebase.auth.currentUser有何区别?@FrankvanPuffelen您完全正确:我将其作为承诺返回,这太过火了。如果您使用的是监听器,onAuthStateChanged监听器应该设置isUser部分,如果您只检查一次,则应该检查。currentUser,如您所说谢谢,我知道,但所有这些解决方案都不适用于我,返回未知值@弗兰克万帕夫伦
async loginStatus() { 
  return firebase.auth().currentUser != null
}