简体   繁体   中英

An error occurred when I get a data from firestore in React app

I'm developing a task management app with Firebase and React.

Current Code

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: {}
  }

Only the code of the relevant part here. Other codes are in Github .

Auth(Firebase) Firestore

The error occured

[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

I tried this to solve the error:

  • Check the network => It is healthy. I can see any pages on the internet and even connect to firebase auth in same program.

  • Use older virsion Firebase => nothing changed.

This is caused because the user is already logged , do something like this and will work:

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

Obs: To use await , you will need a async function

Hope it help:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM