FirebaseError: Failed to get document because the client is offline

Viewed 4811

I am using React 16.13.1

I'm trying to get a document from the 'List' collection. If I comment out lines 15 through 24 and uncomment line 25 it will work.

However, as it is I am getting this error: "FirebaseError: Failed to get document because the client is offline."

The parameter being passed to getDoc() on line 24 is the same as line 25.

Does anyone know why this is happening and how I can get the getDoc() function to work when using line 18?

Update: I've noticed that I can make calls to the database with no error but only after signing in do I get the error "FirebaseError: Failed to get document because the client is offline"

1.   import React, { useState, useEffect } from 'react'
2.   import { Redirect } from 'react-router-dom'
3.   import Firebase from '../../services/Firebase/firebase'
4.   import useStyles from './styles.js'
5.
6.   const Login = ({ currentUser, setCurrentUser }) => {
7.   const [username, setUsername] = useState('')
8.   const [password, setPassword] = useState('')
9.   const [error, setError] = useState(null)
10.
11.  const classes = useStyles()
12.
13.  const handleForm = async e => {
14.    e.preventDefault()
15.    let email = username + '@site.com'
16.    let document
17.    try {
18.      document = await Firebase.doSignInWithEmailAndPassword(email, password)
19.    } catch (err) {
20.      setError('Sorry, there was an issue with your account. Please try again later.')
21.    }
22.    const { user } = document
23.    console.log(user.uid)
24.    getDoc(user.uid)
25.    // getDoc('GeAT8UytRYSax49VNwSYAzFrp7t1')
26.  }
27.
28.  const getDoc = async (uid) => {
29.    try {
30.      const list = await Firebase.database.collection('List').doc(uid).get()
31.      console.log(list)
32.    } catch(err){
33.      console.log(err)
34.    }
35.  }
36.
37.  return (
38.    <>
39.    {/* Some HTML */}
40.    </>
41.  )
42.  }
43.
44.  export default Login
4 Answers

I had the same issue whenever my console was open in chrome in dev mode. Since updating to firebase 7.15.1 I have not had this issue.

I think you need to wait for document

13.  const handleForm = async e => {
14.    e.preventDefault()
15.    let email = username + '@site.com'
16.    let document
17.    try {
18.      document = await Firebase.doSignInWithEmailAndPassword(email, password)
19.    } catch (err) {
20.      setError('Sorry, there was an issue with your account. Please try again later.')
21.    }

22.    if (document) {
23.      getDoc(user.uid)
24.    }

26.  }

I think there are some important things to consider in this case.

  1. Be sure that you have a good internet connection. The client might think that it is online.
  2. You might tried to reference a collection/document which is not yet created. So, You can created the collection with dummy doc or just wait for the document to be created.
  3. Even you can try with simply things as restart the Web Browser.

This can also happen when the port used to connect to the emulator, e.g connectFirestoreEmulator(db, "localhost", <PORT_NUMBER>); does not match the actual port used by the emulator, and defined in the firebase.json file.

Firestore will by default use the port 8080 but it could switch to another port if 8080 is busy.

Related