Home > Mobile >  Firebase.db.collection is not a function || Firebase v9 || throws error when setting collection docu
Firebase.db.collection is not a function || Firebase v9 || throws error when setting collection docu

Time:12-18

I'm working on an Expo React Native project with Firebase v9. For some reason I am able to listen to the "chats" collection in Firestore and successfully console.log them or set them to a variable, but as soon as I try to set them to a piece of state, I get this error:

"TypeError: _firebase.db.collection is not a function. (In '_firebase.db.collection("chats")', '_firebase.db.collection' is undefined)"

For example, this useEffect correctly prints out the array of objects I want to the console:

useEffect(
    () =>
      onSnapshot(collection(db, "chats"), (snapshot) =>
        console.log(
          snapshot.docs.map((doc) => ({
            id: doc.id,
            data: doc.data(),
          }))
        )
      ),
    []
  ); 

But as soon as I swap out the console.log for setChats, I get the above mentioned error.

useEffect(
    () =>
      onSnapshot(collection(db, "chats"), (snapshot) =>
        setChats(
          snapshot.docs.map((doc) => ({
            id: doc.id,
            data: doc.data(),
          }))
        )
      ),
    []
  );

I am correctly importing useState from React as well. I'm also able to set these snapshotted docs to a variable, but if I try to set state using that variable (ie: setChats(myVar);), I get this error.

I've been stuck on this for many hours and cannot for the life of me understand why I'm getting this error. Any ideas? Thanks!

  • Related