Firestore query React Hook throwing error

Viewed 120

I have setup a react hook to query my firestore database. When creating the query it seems to be throwing an overload does not exist error although it looks like my query function has the correct params assigned.

I am using this function to convert an array of constraints passed to the hook into an array of QueryConstraint from the firebase/firestore library which it looks like the query function requires:

function convertArrayQueryClauses(queryConstaints: any[]) {
    let newQueryConstaints: QueryConstraint[] = []
    for(let item of queryConstaints) {
        if(item['where']) newQueryConstaints.push(where(item.where, item.operator, item.value)) 
        if(item['orderBy']) newQueryConstaints.push(orderBy(item.orderBy, item['direction'] ? item.direction : undefined))
        if(item['limit']) newQueryConstaints.push(limit(item.limit))
    }
    return newQueryConstaints
}

in the below consts I build and return the query to use:

const getPathRef = (path: string) => collection(database, path);
const getRefQuery = ({
    path,    
    queryConstraints
}: GetRefQueryParams) => {
    return query(getPathRef(path), convertArrayQueryClauses(queryConstraints))
};

except I am getting the following error:

No overload matches this call.
  Overload 1 of 2, '(query: Query<unknown>, ...queryConstraints: QueryConstraint[]): Query<unknown>', gave the following error.
    Argument of type 'CollectionReference<DocumentData>' is not assignable to parameter of type 'Query<unknown>'.
      Type 'CollectionReference<DocumentData>' is missing the following properties from type 'Query<unknown>': where, orderBy, limit, limitToLast, and 7 more.
  Overload 2 of 2, '(query: Query<DocumentData>, ...queryConstraints: QueryConstraint[]): Query<DocumentData>', gave the following error.
    Argument of type 'QueryConstraint[]' is not assignable to parameter of type 'QueryConstraint'.
      Property 'type' is missing in type 'QueryConstraint[]' but required in type 'QueryConstraint'.ts(2769)
1 Answers

I believe just missing the ... operator!

return query(getPathRef(path), ...convertArrayQueryClauses(queryConstraints))

seems to work fine!

Related