我第一次嘗試設定Firebase,但出現了這些錯誤,我找不到任何正確的答案,這是我的配置
。//import the functions you need from the SDKs you need.
import firebase from 'firebase/compat/app'。
import 'firebase/firestore';
import {
初始化應用
}
from "firebase/app"。
// TODO:為你想使用的Firebase產品添加SDK。
// https://firebase.google.com/docs/web/setup#available-libraries
//你的Web應用的Firebase配置
const firebaseConfig = {
apiKey: "x"。
authDomain: "x",
projectId: "x",
storageBucket: "x",
messagingSenderId: "x",
appId: "x", appId:
};
//初始化Firebase。
const app = initializeApp(firebaseConfig)。
const db = firebase.firestore()。
export {
db
};
我得到的錯誤是 - TypeError: firebase_compat_app__WEBPACK_IMPORTED_MODULE_0__.default.firestore is not a function
它顯示在一行 const db = firebase.firestore();/p>
uj5u.com熱心網友回復:
如果你想使用Firestore的compat版本,你需要將firebaseApp也初始化為compat版本。我建議在這兩方面都使用新的SDK版本:
。
import { getFirestore } from "firebase/firestore"。
import { initializeApp } from "firebase/app"。
const firebaseConfig = {
apiKey: "x"/span>,
authDomain: "x",
projectId: "x",
storageBucket: "x",
messagingSenderId: "x",
appId: "x"。
};
const app = initializeApp(firebaseConfig)。
const db = getFirestore()。
export { db };
在新的SDK中,你實際上不需要像以前那樣在初始化你的應用程式和創建資料庫實體時需要這樣一個檔案。在你用新的SDK初始化你的應用程式后,你可以直接呼叫getFirestore()而不需要為它準備firebaseApp。getFirestore()將自動使用默認的應用程式。
一個通過使用collection的實時監聽器將看起來像這樣:
import { collection, onSnapshot } from "firebase/firestore"。
const unsubscribe = onSnapshot(collection(db, "cities"), ()=> {
//回應資料。
// ...
});
//后來 ...
//停止聆聽變化。
unsubscribe()。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/325761.html
標籤:

