1

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; 

1 Answer 1

4

The collection is a method, not an array. So:

db.collection('posts').onSnapshot(... // 👆 👆 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.