Home > Mobile >  getDoc from firestore without searching for specific uid
getDoc from firestore without searching for specific uid

Time:05-16

I'm having an issue getting the collection info. Using this code, I could only get the doc with a specific uid.

const snapShot = doc(db, 'Files', 'someUID');
const getSnapShot = await getDoc(snapShot);

console.log(getSnapShot.data());

I used this code to get all of the collection items but threw an error.

const snapShot = collection(db, 'Files');
const getSnapShot = await getDoc(snapShot);

console.log(getSnapShot.forEach(doc => console.log(doc.data()));


Uncaught (in promise) FirebaseError: Expected type 'wc2', but it was: a custom gc2 object

Q: How do I make it work?

CodePudding user response:

To get all documents from a collection or documents that match a query you should use getDocs() instead of getDoc() that is used to fetch a single document as the function name suggests.

const snapShot = collection(db, 'Files');
const getSnapShot = await getDocs(snapShot);

console.log(getSnapShot.forEach(doc => console.log(doc.data()));
  • Related