Firestore查询React Hook抛出错误



我已经设置了一个react钩子来查询我的firestore数据库。当创建查询时,它似乎抛出了一个重载不存在的错误,尽管它看起来像我的查询函数有正确的参数分配。

我使用这个函数将传递给钩子的约束数组转换为来自firebase/firestore库的QueryConstraint数组,它看起来像查询函数需要:

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
}
在下面的const中,我构建并返回查询:
const getPathRef = (path: string) => collection(database, path);
const getRefQuery = ({
path,    
queryConstraints
}: GetRefQueryParams) => {
return query(getPathRef(path), convertArrayQueryClauses(queryConstraints))
};

除了我得到以下错误:

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)

我相信只是错过了…运营商!

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

似乎可以正常工作!

相关内容

最新更新