Reactjs React with React redux firebase isLoaded为true,isEmpty似乎为false,但firebase.auth().currentUser为null-可能是什么原因?

Reactjs React with React redux firebase isLoaded为true,isEmpty似乎为false,但firebase.auth().currentUser为null-可能是什么原因?,reactjs,firebase,firebase-authentication,react-router-dom,react-redux-firebase,Reactjs,Firebase,Firebase Authentication,React Router Dom,React Redux Firebase,因此,我可能很难解释这个问题,我无法始终如一地重现。我有一个React应用程序,我正在使用它React-redux firebase,我认为我成功地实现了该应用程序以跟踪用户会话 My App.js文件具有以下位或路由代码作为示例(使用react router dom): 我正在导入isLoaded,如下所示: import { firebaseConnect, isLoaded } from 'react-redux-firebase'; 该条件工作正常,用户登录,然后出现isLoaded条

因此,我可能很难解释这个问题,我无法始终如一地重现。我有一个React应用程序,我正在使用它
React-redux firebase
,我认为我成功地实现了该应用程序以跟踪用户会话

My App.js文件具有以下位或路由代码作为示例(使用
react router dom
):

我正在导入isLoaded,如下所示:

import { firebaseConnect, isLoaded } from 'react-redux-firebase';
该条件工作正常,用户登录,然后出现
isLoaded
条件-这就是问题所在。如果isLoaded为true,我会假设用户和所有redux firestore用户属性都已准备好使用……但有时情况并非如此。在navigator.setGeoLocation调用中,我有以下内容:

setGeoLocation(propsFirebase, cb) {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition((position) => {
            propsFirebase.auth().currentUser
                .getIdToken(/* forceRefresh */ true)
                .then((idToken) => {
                    ...
                });
            }
        )
    }
}
此时,propsFirebase.auth().currentUser有时为null(这源于在
navigator.setGeoLocation(this.props.firebase);
中传递的参数)。如果我再次尝试登录,那么它会起作用。我无法找到任何一致的复制方式

我在其他组件中也注意到了这一点。我不确定这是否是一个问题,当我的电脑进入睡眠状态,我应该重新启动整个反应过程或什么?有人见过类似的问题吗?如果是这样,在路由中检查用户状态时,我可能会遗漏什么

如果需要更多代码,请告诉我……

将为空,并且用户未登录。当页面首次加载时,在加载用户的令牌且用户对象可用之前,它也将为null。如果要在页面加载后异步加载用户对象后立即执行操作,则应使用获取用户对象

firebase.auth().onAuthStateChanged((user) => {
  if (user) {
    // User is signed in, see docs for a list of available properties
    // https://firebase.google.com/docs/reference/js/firebase.User
    var uid = user.uid;
    // ...
  } else {
    // User is signed out
    // ...
  }
});

您可能还想阅读更多详细信息:

情况并非如此,这是随机的吗?或者它总是发生吗?似乎是随机的…它发生过一次,然后就再也不会发生了。这很好,似乎这就是发生在我身上的事情-问题:如果(用户)-用户与firebase.auth().currentUser是同一个对象吗?我是否可以假设如果设置了user,那么firebase.auth().currentUser也将被设置?如果您从observer函数获得回调,则可以确保在回调完成后currentUser将不为null。在回调中,应该使用提供的用户对象。
setGeoLocation(propsFirebase, cb) {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition((position) => {
            propsFirebase.auth().currentUser
                .getIdToken(/* forceRefresh */ true)
                .then((idToken) => {
                    ...
                });
            }
        )
    }
}
firebase.auth().onAuthStateChanged((user) => {
  if (user) {
    // User is signed in, see docs for a list of available properties
    // https://firebase.google.com/docs/reference/js/firebase.User
    var uid = user.uid;
    // ...
  } else {
    // User is signed out
    // ...
  }
});