reactjs - 无法连接到 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}`);
});
},[]);
解决方案
问题实际上出在我的文件中,我在变量周围加上.env
了引号。"
这导致它们未定义,因此无法连接。这个问题的答案帮助了我
推荐阅读
- javascript - Laravel 项目调用了错误的路径
- java - 在 Android 中使用 Spannable 获取完整的 EditText
- python - 如何在 Python 中使用 Selenium 处理自动建议下拉菜单
- modx - 在 MODX 使用 modRestController 类发布
- python - 如何将另一个列表的当前状态而不是迭代后的最终状态附加到列表中?
- filter - 是否可以在 pandoc 过滤器上启用扩展?
- sql - 红移 Postgres 8
- python - Remove list items from within list of list
- javascript - 打字稿中的导入语句
- c++ - 枚举,命名空间和命名空间之外的声明导致“重载(...)的调用不明确”