【问题标题】:FirebaseError: Failed to get document because the client is offlineFirebaseError:无法获取文档,因为客户端处于脱机状态
【发布时间】:2020-09-28 04:27:35
【问题描述】:

我正在使用 React 16.13.1

我正在尝试从“列表”集合中获取文档。 如果我注释掉第 15 到 24 行并取消注释第 25 行,它将起作用。

但是,我收到了这个错误: “FirebaseError:无法获取文档,因为客户端离线。”

第 24 行传递给 getDoc() 的参数与第 25 行相同。

有谁知道为什么会发生这种情况以及如何在使用第 18 行时让 getDoc() 函数工作?

更新:我注意到我可以毫无错误地调用数据库,但只有在登录后我才会收到错误“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

【问题讨论】:

    标签: reactjs firebase google-cloud-firestore react-hooks


    【解决方案1】:

    每当我的控制台以开发模式在 chrome 中打开时,我都会遇到同样的问题。自从更新到 firebase 7.15.1 我没有遇到这个问题。

    【讨论】:

    • 仅供参考并确认它是issue with firebase。 7.15.1 表明它修复了这个特定问题。
    • 发生在"firebase": "^8.2.7",
    【解决方案2】:

    我认为你需要等待文件

    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.  }
    

    【讨论】:

    • 感谢您的反馈。我可以确认 user.uid 每次都按照我最初编写的方式传递给 getDoc 函数。
    【解决方案3】:

    我认为在这种情况下需要考虑一些重要的事情。

    1. 确保您的互联网连接良好。客户可能会认为它在线。
    2. 您可能试图引用尚未创建的集合/文档。因此,您可以使用虚拟文档创建集合,也可以等待文档创建完成。
    3. 您甚至可以尝试简单的操作,例如重新启动 Web 浏览器。

    【讨论】:

    • 感谢您的反馈。我正在开发中对此进行测试,所以所有这些都被考虑在内。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-01
    • 2018-03-19
    • 2016-02-07
    • 2019-05-04
    • 1970-01-01
    • 2013-05-07
    • 1970-01-01
    相关资源
    最近更新 更多