I'm trying to access all documents from my firestore collection
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
async function getTodos() {
try {
const todoRef = collection(db, 'todos');
let allTodos = await getDoc(todoRef);
console.log(allTodos)
} catch (err) {
console.log(err)
}
}
but it's throwing this err
FirebaseError: Expected type 'Tc', but it was: a custom Ac object
CodePudding user response:
The getDoc()
method is used to get a single document and takes a DocumentReference as a parameter. Here todoRef
is a CollectionReference and to fetch multiple documents you must use getDocs()
instead:
const todoRef = collection(db, 'todos');
let allTodos = await getDocs(todoRef);