Reputation: 59
Im am trying to collect data from firebase database but get the error 'TypeError: Cannot read properties of undefined (reading '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;
Upvotes: 1
Views: 1512
Reputation: 599956
The collection
is a method, not an array. So:
db.collection('posts').onSnapshot(...
// 👆 👆
Upvotes: 4