11

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

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
Rohit Dhas
  • 207
  • 1
  • 3
  • 9

4 Answers4

19

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);
Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
3

You have used getDoc() method which helps to get one single document so just replace your getDoc() with getDocs()

2

Get only one document:

import { doc, getDoc , getDocs } from "firebase/firestore";

const docRef = doc(db, "cities", "SF");
const docSnap = await getDoc(docRef);

Get multiple documents from a collection

import { doc, getDoc , getDocs } from "firebase/firestore";

const docRef = doc(db, "cities");
const docs = await getDocs(q);

0

I got this same error this week, and the issue was that I was importing from two different versions of the firebase/firestore package... One version in my application, and a different version in the data layer which was in a separate NPM module.

Building that NPM module properly so that firebase/firestore was a peer dependency got the getDoc and collection functions using the same version of Firestore, which resolved the issue for me.

Erik Pukinskis
  • 480
  • 5
  • 11