Home > Software engineering >  TS Error: Type 'Query<DocumentData>' is not assignable to parameter of type 'Do
TS Error: Type 'Query<DocumentData>' is not assignable to parameter of type 'Do

Time:11-20

So I am trying to run a query against Firestore data. My code is:

import { collection, getDoc, query, where } from "firebase/firestore";
import { db } from "../../utils/firebaseConfig";

  const getQuery = async () => {
    const userDataRef = collection(db, "userData");
    const q = await query(
      userDataRef ,
      where("mailID", "==", "[email protected]")
    );

    const users = await getDoc(q) //error

  };

  console.log(getQuery());

In line const users = await getDoc(q), I'm getting this error:

//error description- Argument of type 'Query<DocumentData>' is not assignable to parameter of type 'DocumentReference<DocumentData>'.

code screenshot with error

I tried searching the error but I only got different variant and couldn't find usefull answers to help myself.

I am new to TS and learning by using TS in my projects. I will be really gratefull if you can guide me through this.

CodePudding user response:

The getDoc() function is used to fetch a single document using DocumentReference. For fetching multiple documents with a Query, use getDocs():

const users = await getDocs(q);
  • Related