当我在React应用中从firestore获取数据时,发生了一个错误。

问题描述 投票:1回答:1

我正在用Firebase和React开发一个任务管理应用。

当前代码

export const login = (type, email, password) => dispatch => {
  dispatch({type: "LOGIN"});
  switch (type) {
    case "EMAIL": {

      // Logging in using Firebase Auth. It goes successfully.

      firebase.auth().signInWithEmailAndPassword(email, password).then(result => { 
        const db = firebase.firestore();
        const uid = result.user.uid

      // Getting user name from Firestore. The fllowing error occurs here.

        let name;
        db.collection("users").get().then(querySnapshot => {
          querySnapshot.forEach(doc => {
            name = doc.name;
            dispatch({type: "LOGIN_SUCCEEDED", uid: uid, name: name,});
            dispatch(push("/"))
          });
        }).catch(err => {
          dispatch({type: "LOGIN_REJECTED", error: err});
        });
      }).catch(err => {
        dispatch({type: "LOGIN_REJECTED", error: err.message});
      });
      break;
    }

    default: {}
  }

此处只填写相关部分的代码,其他代码见于 Github.

Auth(Firebase)火库

错误发生在

[2020-06-07T03:54:57.645Z]  @firebase/firestore: Firestore (7.15.0): Could not reach Cloud Firestore backend. Connection failed 1 times. Most recent error: FirebaseError: [code=unknown]: Fetching auth token failed: getToken aborted due to token change.
This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend. index.js:1

我试过这样解决这个错误。

  • 检查网络 => 它是健康的。我可以在互联网上看到任何网页,甚至可以在同一个程序中连接到Firebase auth。

  • 使用旧版本的Firebase => 没有任何改变。

reactjs firebase
1个回答
0
投票

这是因为 用户已登录,做这样的事情,会有效果。

if (!firebase.auth().currentUser) {
  await firebase.auth().signInWithEmailAndPassword(email, password);
}

观察: 要使用 等待,你需要一个 异步 功能

希望能帮到你:)

© www.soinside.com 2019 - 2024. All rights reserved.