[英]TypeError: Cannot read properties of undefined (reading 'onSnapshot')
Im am trying to collect data from firebase database but get the error 'TypeError: Cannot read properties of undefined (reading 'onSnapshot')'我正在尝试从 firebase 数据库收集数据,但收到错误“TypeError:无法读取未定义的属性(读取“onSnapshot”)”
Feed.js 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 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;
The collection
is a method, not an array. collection
是一种方法,而不是数组。 So:所以:
db.collection('posts').onSnapshot(...
// 👆 👆
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.