首页 > 解决方案 > 无法连接到 Firestore 数据库

问题描述

尝试从 Firestore 获取文档及其字段时,出现以下错误:

从本地主机:

@firebase/firestore: Firestore (8.3.2): Could not reach Cloud Firestore backend. Connection failed 1 times. Most recent error: FirebaseError: [code=permission-denied]: Permission denied on resource project "xxxxxxxxx".
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.

从 Firebase 托管:

BrowserPollConnection.ts:728 GET https://[PROJECT_ID]-default-rtdb.firebaseio.com/.lp?start=t&ser=82934841&cb=1&v=5&p=1:303920306737:web:bc61250d7aa9a51a415310 net::ERR_NAME_NOT_RESOLVED

在查找此问题时,解决方案似乎是缓存、DNS、ISP、VPN 或类似的东西。我已经尝试了所有建议的解决方案,但仍然没有解决方案。我在想这可能是我配置连接的方式?

我唯一觉得奇怪的是,它声明它试图访问https://[PROJECT_ID]-default-rtdb.firebaseio.com,我认为这仅适用于 RTDB,但我使用的是 Firestore。

在我的配置文件中,我有以下内容:

firebase/index.js

import firebase from 'firebase/app';
import 'firebase/firestore';

var config = {
  apiKey: process.env.REACT_APP_API_KEY,
  authDomain: `${process.env.REACT_APP_PROJECT_ID}.firebaseapp.com`,
  projectId: process.env.REACT_APP_PROJECT_ID,
  storageBucket: process.env.REACT_APP_STORAGE,
  messagingSenderId:process.env.REACT_APP_MESSAGE_SENDER_ID,
  appId:process.env.REACT_APP_APP_ID,
  measurementId: process.env.REACT_APP_MEASUREMENT_ID
  
}

firebase.initializeApp(config);

export default firebase;

我试图只在useEffect. 奇怪的是,一旦它返回setData,执行 data.id 会打印出实际的 ID,但是在useEffect它内部或外部检查文档是否存在docSnapshot.exists总是错误的。可能是因为我的连接问题。

import firebase from './firebase';
const db = firebase.firestore();
.
.
.

    useEffect(() => {
           
    
    
            db.collection('myCollection').doc('myDocID')
            .onSnapshot(docSnapshot => {
    
                console.log(`Received doc snapshot: ${docSnapshot}`);
                setData(docSnapshot);
    
            }, err => {
                console.log(`Encountered error: ${err}`);
    
            });
    },[]);

标签: reactjsfirebasefirebase-realtime-databasegoogle-cloud-firestorefirebase-authentication

解决方案


问题实际上出在我的文件中,我在变量周围加上.env了引号。"这导致它们未定义,因此无法连接。这个问题的答案帮助了我

反应环境变量.env返回未定义


推荐阅读