【发布时间】:2021-12-01 22:02:58
【问题描述】:
我无法弄清楚这里的错误是什么。我制作了教程 Firebase/website,并在我的项目中做了同样的事情。我试图在我的 React 网站上打印出我在 Firebase 上创建的集合,但在 db.collection 行出现错误:Uncaught (in promise) TypeError: ae.collection is not a function
component.js:
// React
import { useState, useEffect } from "react";
import { db, collection, getDocs } from '../../../firebase.js';
const [holder, setHolder] = useState([]); // update
db.collection("holder").onSnapshot((snapshot) => {
setHolder(
snapshot.docs.map((doc) => ({
id: doc.id,
data: doc.data(),
}))
);
});
console.log({ holder });
更新:firebase.js 下面
// Import the functions you need from the SDKs you need
import { initializeApp } from "firebase/app";
import { getAnalytics } from "firebase/analytics";
import { getFirestore, collection, getDocs } from 'firebase/firestore/lite';
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries
// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
...
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
export const db = getFirestore(app);
谢谢!
【问题讨论】:
-
你能分享你的
firebase.js文件吗? -
完成!谢谢
标签: javascript reactjs firebase google-cloud-firestore