首页 > 解决方案 > Firebase 在 onAuthStateChange 之后获取子级但出现错误客户端处于脱机状态

问题描述

使用firebase onAuthStateChange后如何在实时数据库中获取用户实例?
我在我的 useEffect 中尝试过这样的操作,但出现错误Client is Offline
我很确定我的连接良好,firebase 也在线
Btw 如果我删除get(child())代码,代码正在工作
我需要这个 get child用于获取用户实例以将用户数据存储到初始屏幕内的本地存储
注意:Firebase v9.1.1 + React Native 0.66.0

  useEffect(() => {
    const auth = getAuth(Firebase);
    const dbRef = ref(getDatabase(Firebase));
    const unsubscribe = navigation.addListener('focus', () => {
      onAuthStateChanged(auth, user => {
        setTimeout(() => {
          if (user) {
            console.log('Logged => ', user);
            get(child(dbRef, `users/${user.uid}`)).then(snapshot => {
              if (snapshot.val()) {
                console.log('User In Db...');
                storeLocalStorage('user', snapshot.val());
                navigation.replace('MainApp');
              } else {
                console.log('User Not In DB..');
                removeLocalStorage('user');
                navigation.replace('GetStarted');
              }
            });
          } else {
            console.log('Not Logged In');
            removeLocalStorage('user');
            navigation.replace('GetStarted');
          }
        }, 3000);
      });
    });

    return unsubscribe;
  }, [navigation]);

标签: javascriptreactjsfirebasereact-nativefirebase-realtime-database

解决方案


推荐阅读