Firebase onAuthStateChanged unsubscribe is not a function 错误

标签 firebase react-native firebase-authentication react-navigation react-native-firebase

首次安装时,应用程序运行没有任何问题。

  1. 在启动画面检查登录状态。
  2. 用户未登录,导航到登录屏幕。

unsubscribe() 不是函数 当应用程序最小化然后重新打开时,错误显示并停留在启动画面。这只发生在最小化 > 从全新安装和首次运行恢复时。

这意味着如果我从后台终止应用程序并打开然后最小化然后恢复,它工作得很好。

这是我在 firebase 文档中使用的 checkLoginState 函数。我不确定这与 react-nativefirebasereact-native-firebasereact-navigation 有关.知道如何调试和解决这个问题吗?

Handle the sign-in flow manually

function checkLoginState(event) {
  if (event.authResponse) {
    // User is signed-in Facebook.
    var unsubscribe = firebase.auth().onAuthStateChanged(function(firebaseUser) {
      unsubscribe();
      // Check if we are already signed-in Firebase with the correct user.
      if (!isUserEqual(event.authResponse, firebaseUser)) {
        // Sign in user.
      } else {
        // User is already signed-in Firebase with the correct user.
      }
   });
  } else {
    // User is signed-out of Facebook.
    firebase.auth().signOut();
  }
}

更新

在全新安装 > 首次运行 > 最小化 > 恢复` 中,应用程序看起来像堆栈或什么。如果我使用 react-native reload 重新加载应用程序,我会在日志中得到这个。 componentDidmount 和 unmount 运行多次。每个最小化 > 恢复堆栈 1。如果我终止应用程序并重新打开,这个问题就会消失。请注意,这在物理和虚拟设备中都会发生。

enter image description here

更新 1

这主要与 React Native 有关。 https://github.com/facebook/react-native/issues/12562

最佳答案

我也遇到了这个问题并且对此感到很沮丧...根据调用监听器作为函数的文档必须取消订阅监听器,但事实并非如此。所以这就是我所做的,希望对你也有帮助..

class Home extends Component {


constructor(props) {
    super(props);
    this.Homelistener = null;
    this.state = {
          // ...
    };


this.Homelistener = firebase.auth().onAuthStateChanged(user => {
  if (user) {

   //do whatever you want here
   // you can unsubscribe from the listener here or do it in 
   //componentwillunmount()

    this.Homelistener = null; // --> just set it to null

  } else {

    //do something

    SplashScreen.hide();

  }
});

关于Firebase onAuthStateChanged unsubscribe is not a function 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48230707/

相关文章:

javascript - 在react-native TextInput中parseInt()之后值为NaN

ios - iOS 上的 React-native : cannot adjust current top of stack beyond available views

android - 无法从包外部访问电话验证卡

firebase - 为 Firebase 的 Cloud Functions 设置超时不会保留在控制台中;这是一个错误吗?

javascript - 是否可以在实际获取之前检查 onSnapshot() 是否发现更改?

android - Firebase - 排除一些屏幕进行跟踪

java - Android Firebase : Cannot figure out how to use GSON to make pojo

php - 从 MySQL 检索数据到 React Native 时获取速度缓慢

android - Firebase 用户在调用 updateProfile() 方法后不再匿名

firebase - 如何根据 key 对 firebase 控制台中的节点进行排序