【问题标题】:TypeError: _firebase_config__WEBPACK_IMPORTED_MODULE_1__.db.collection is not a function firebase类型错误:_firebase_config__WEBPACK_IMPORTED_MODULE_1__.db.collection 不是函数 firebase
【发布时间】:2021-11-09 05:54:25
【问题描述】:

出现错误的代码:

const user = db.collection("users").doc(`${match.params.id}`).get();

错误:

TypeError: _firebase_config__WEBPACK_IMPORTED_MODULE_1__.db.collection is not a function

这是firebase-config.js

 // Import the functions you need from the SDKs you need
import { initializeApp } from "firebase/app";
import { getAnalytics } from "firebase/analytics";
import { getAuth } from "firebase/auth";
import { getFirestore } from "firebase/firestore";

const firebaseConfig = {
   apiKey: "xxxxxxxxxx",
   authDomain: "xxxxxxx.firebaseapp.com",
   projectId: "PROJECT-ID",
   storageBucket: "xxxxxxxxxx.appspot.com",
   messagingSenderId: "xxxxxxxxx",
   appId: "xxxxxxxxxxx",
   measurementId: "xxxxxxxxx",
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
const db = getFirestore(app);
const auth = getAuth(app);
export { analytics, auth, db };

尝试

重新安装 Firebase,不行 重新安装所有模块(删除 node_modules 文件夹和 npm install ),Nopes

【问题讨论】:

  • 这是一个普通的firebase应用程序,不是firebase管理员,我使用的是Firebase 9.0.2,所以语法不同+我没有使用实时数据库我使用的是Firestore

标签: reactjs firebase google-cloud-firestore


【解决方案1】:

对于 Firebase Modular 或 9.0.0 及更高版本,您应该在查询中使用新的实现,for example

之前:版本 9 compat 或版本 8 及以下

import "firebase/compat/firestore"

const db = firebase.firestore();
db.collection("cities").where("capital", "==", true)
    .get()
    .then((querySnapshot) => {
        querySnapshot.forEach((doc) => {
            // doc.data() is never undefined for query doc snapshots
            console.log(doc.id, " => ", doc.data());
        });
    })
    .catch((error) => {
        console.log("Error getting documents: ", error);
    });

之后:版本 9 模块化

import { getFirestore, collection, query, where, getDocs } from "firebase/firestore";

const db = getFirestore(firebaseApp);

const q = query(collection(db, "cities"), where("capital", "==", true));

const querySnapshot = await getDocs(q);
querySnapshot.forEach((doc) => {
  // doc.data() is never undefined for query doc snapshots
  console.log(doc.id, " => ", doc.data());
});

在你的情况下,应该是:

const docRef = doc(db, "users", `${match.params.id}`);
const user = await getDoc(docRef);

【讨论】:

  • 以下是否正确:const user = auth.currentUser; const userRef = doc(db, "users", ${user.uid}); const userSnapshot = await getDoc(userRef); await setDoc(userSnapshot, { name: user.displayName, email: user.email, photoURL: user.photoURL, uid: user.uid, createdAt: new Date(), updatedAt: new Date(), role: "USER", });
  • 我收到以下错误:FirebaseError: Expected type 'Ia', but it was: a custom Cu object
  • @HridayaAgrawal 删除 const userSnapshot = await getDoc(userRef); 并将 setDoc(userSnapshot 替换为 setDoc(userRef
猜你喜欢
  • 1970-01-01
  • 2016-04-22
  • 2021-11-17
  • 1970-01-01
  • 2021-10-31
  • 2021-01-25
  • 1970-01-01
  • 2022-10-07
相关资源
最近更新 更多