最初的Firebase 9连接被拒绝



尝试学习Firebase以添加到我的React项目中。许多答案都与旧版本的Firebase有关。读到Firebase 9是一个重大更新,所以很难找到答案。我一直收到这个错误:

fetchxmlhttpfactory.js:270          POST http://localhost:8090/google.firestore.v1.Firestore/Listen/channel?database=projects%2Fabc-chart%2Fdatabases%2F(default)&VER=8&RID=41901&CVER=22&X-HTTP-Session-Id=gsessionid&%24httpHeaders=X-Goog-Api-Client%3Agl-js%2F%20fire%2F9.6.10%0D%0AContent-Type%3Atext%2Fplain%0D%0AX-Firebase-GMPID%3A1%3A177213787941%3Aweb%3A9f6ce48e7d16c61c3754cc%0D%0A&zx=7tsvfv2qwcyt&t=1 net::ERR_CONNECTION_REFUSED

这个:

@firebase/firestore: Firestore (9.6.10): Could not reach Cloud Firestore backend. Connection failed 1 times. Most recent error: FirebaseError: [code=unavailable]: The operation could not be completed
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-config.js文件,按照他们的建议从firebase复制的。

import { initializeApp } from 'firebase/app'
import { getFirestore, connectFirestoreEmulator } from '@firebase/firestore'
const firebaseConfig = {
apiKey: "",
authDomain: "",
projectId: "",
storageBucket: "",
messagingSenderId: "",
appId: "",
measurementId: ""
};
// init firebase app
const app = initializeApp(firebaseConfig);
// init services: used to reach out to get data
export const db = getFirestore(app);
connectFirestoreEmulator(db, 'localhost', 8090);

和React文件App.js

import { useState, useEffect } from 'react';
import './App.css';
import { db } from './firebase-config';
import { collection, getDocs } from 'firebase/firestore';
function App() {
const [students, setStudents] = useState([]);
const studentCollectionRef = collection(db, "students");

useEffect(() => {
const getStudents = async () => {
const data = await getDocs(studentCollectionRef);
console.log(data);
}
getStudents()
}, [])
return (
<div className="App">
<header className="App-header">
Hey queen
</header>
</div>
);
}
export default App;

根据您提供的内容,我可以建议三件事:

  • 如果您在配置中使用env-var,请确保它们以REACT_APP开头。例如:

apiKey: process.env.REACT_APP_API_KEY

  • 通过运行firebase emulators:start(可选firebase emulators: start --import ./path/to/firestore-data(,确保模拟器在connectFirestoreEmulator之前运行

  • 确保connectFirestoreEmulator(8090(中指定的端口与firebase.json文件中指定的相同。

最新更新