[英]TypeError: Cannot read properties of undefined (reading 'onSnapshot')
我正在嘗試從 firebase 數據庫收集數據,但收到錯誤“TypeError:無法讀取未定義的屬性(讀取“onSnapshot”)”
Feed.js
const Feed = ({profilePic, message, timestamp, username, image}) => {
const [posts, setPosts] =useState([]);
//realtime database connection
useEffect(() => {
db.collection['posts'].onSnapshot(snapshot => (
setPosts(snapshot.docs.map((doc) => ({ id: doc.id, data: doc.data() })))
));
}, []);
return (
Firebase.js
import firebase from 'firebase/compat/app';
import 'firebase/compat/firestore';
//initialize Firebase
firebase.initializeApp(firebaseConfig);
//database
const db = firebase.firestore();
export default db;
collection
是一種方法,而不是數組。 所以:
db.collection('posts').onSnapshot(...
// 👆 👆
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.