我正在嘗試使用 firebase 為我的網路制作簡單的登錄功能,但是我收到一條錯誤訊息:
ERROR db._checkNotDeleted is not a function
我的 Firebase 版本是 9,而 React 在 17.0.2 上運行。
這是我的 firebase 組態檔片段(將隱藏配置,因為它不必要):
import { initializeApp } from "firebase/app";
import { getAuth, signInWithPopup, GoogleAuthProvider } from "firebase/auth";
import { getDatabase, ref, set } from "firebase/database";
import { getFirestore, doc, getDoc, collection, addDoc, getDocs, query, where} from "firebase/firestore";
const firebaseConfig = {
*/*/*/*/
};
const app = initializeApp(firebaseConfig);
export const auth = getAuth(app);
export const createUserProfileDocument = async (userAuth, additionalData) => {
if(!userAuth) return;
const docRef = doc(db, "users", `${userAuth.uid}`);
const docSnap = await getDoc(docRef);
if (!docSnap.exists()) {
const { displayName, email } = userAuth;
const createdAt = new Date();
try {
await set(ref(db, 'users/' `${userAuth.uid}`), {
displayName: "TEST"
})
console.log("HERE");
} catch (error) {
console.log("ERROR", error.message);
}
}
return docRef;
}
const provider = new GoogleAuthProvider();
export const signInWithGoogle = () => {
signInWithPopup(auth, provider)
.then((result) => {
console.log(result);
}).catch((error) => {
console.log(error);
})
}
const db = getFirestore();
這是我的專案:https : //github.com/poolpy111/oto-clothing/tree/master/src
無法真正弄清楚這是什么錯誤試圖說,所以我真的很困惑。在除錯器錯誤行出現在第 43 行,這是我使用 firebase 的“ref”方法的地方。我該怎么辦?提前致謝
uj5u.com熱心網友回復:
Firebase 有兩個資料庫,即Firebase Realtime Database和Cloud Firestore。在提供的代碼中,您通過使用但隨后使用從導致錯誤的實時資料庫 SDK 匯入的來創建DocumentReference。doc()set()
如果要在 Firestore 中添加資料,請setDoc()從 Firestore匯入并重構代碼,如下所示:
import { initializeApp } from "firebase/app";
import { getAuth, signInWithPopup, GoogleAuthProvider } from "firebase/auth";
import { getFirestore, doc, getDoc, setDoc, collection, addDoc, getDocs, query, where} from "firebase/firestore";
export const createUserProfileDocument = async (userAuth, additionalData) => {
if(!userAuth) return;
const docRef = doc(db, "users", `${userAuth.uid}`);
const docSnap = await getDoc(docRef);
if (!docSnap.exists()) {
const { displayName, email } = userAuth;
const createdAt = new Date();
try {
// use setDoc()
await setDoc(docRef, {
displayName: "TEST"
})
console.log("HERE");
} catch (error) {
console.log("ERROR", error.message);
}
}
return docRef;
}
請按照Firestore 的檔案了解更多資訊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/386073.html
標籤:javascript 反应 火力基地 谷歌云firestore
