简体   繁体   English

firebase.auth()。onAuthStateChanged返回什么?

[英]what is returned on firebase.auth().onAuthStateChanged?

When I hover over onAuthStateChanged it shows this: 当我将鼠标悬停在onAuthStateChanged它将显示以下内容:

(method) firebase.auth.Auth.onAuthStateChanged(nextOrObserver: firebase.Observer<any, any> | ((a: firebase.User) => any), error?: (a: firebase.auth.Error) => any, completed?: firebase.Unsubscribe): firebase.Unsubscribe

It also seems to work if I do user: firebase.User 如果我执行user: firebase.User它也似乎可以正常工作

  firebase.auth().onAuthStateChanged((user) => {
    console.log("data from stateChanged: ", user);
  })

But when I try to access the properties of user, ie user.uid then I get error basically saying inaccessible. 但是,当我尝试访问用户的属性,即user.uid时,我得到的错误基本上是说无法访问。 So what exactly is the return type here and how do I access that? 那么,这里的返回类型到底是什么,我该如何访问呢?

onAuthStateChanged returns either a firebase.User or null . onAuthStateChanged情况传回firebase.Usernull You need to null guard it and potentially cast as a firebase.User in Typescript before accessing it's attributes. 你需要为null保护它,并可能铸成firebase.User在打字稿访问它的属性了。

I'd recommend using the AngularFire2 and it's AngularFireAuth module, if you're using Angular; 如果您使用的是Angular,我建议您使用AngularFire2及其AngularFireAuth模块。 we've smoothed over some of these API concerns and have wrapped the callbacks in RxJS Observables. 我们已经解决了其中一些API问题,并将回调包装在RxJS Observables中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从firebase.auth()。onAuthStateChanged中提取数据 - How to extract the data from a firebase.auth().onAuthStateChanged Firebase:如何从 firebase.auth().onAuthStateChanged 异步绑定用户到 Angular 模板? - Firebase: How to asynchronously bind User from firebase.auth().onAuthStateChanged to Angular template? firebase.auth()。currentUser为null - firebase.auth().currentUser is null firebase 或 firebase auth 中的 onAuthStateChanged() 不返回值 - onAuthStateChanged() in firebase or firebase auth does not return values 在firebase.auth()类型脚本中访问全局变量 - accessing global variables in firebase.auth() typescript 如何将 firebase.auth 导入到 angular 10 - how to import firebase.auth to angular 10 更新 Firebase 和 Angular 后获取“firebase.auth is not a function” - Getting “firebase.auth is not a function” after updating Firebase and Angular firebase.auth()。当刷新页面时,currentUser返回null - firebase.auth().currentUser returns null when page is refreshed 使用Firebase,即使正在记录firebase.auth()。currentUser,它也是一个空值 - Using Firebase, firebase.auth().currentUser is a null value even though it is being logged firebase.auth().createUserWithEmailAndPassword() - 在使用任何其他 rxJs 运算符之前完成 - firebase.auth().createUserWithEmailAndPassword() - completes before a using any other rxJs operators
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM