[英]Error: A non-empty array is required for 'in' filters
I want to create custom hook that will run react-firebase-hooks library hook: useCollection .我想创建将运行 react-firebase-hooks 库钩子的自定义钩子: useCollection 。 I use redux toolkit in my project as a state manager, and redux provides my custom hook with the game data.
我在我的项目中使用 redux 工具包作为 state 经理,redux 为我的自定义挂钩提供游戏数据。 I need to grab connected players IDs from store:
game.players: string[]
.我需要从商店获取连接的玩家 ID:
game.players: string[]
。 But the thing is that sometimes game won't be fetched still, sometimes it cab be null
and new created game does not have any connected players.但问题是有时游戏仍然不会被提取,有时它可能是
null
并且新创建的游戏没有任何连接的玩家。
I've created my own implementation:我创建了自己的实现:
export const useGamePlayers = () => {
const {game} = useTypedSelector(state => state.game);
return useCollection(
query(collection(db, 'players'), where(documentId(), 'in', game?.players))
)
};
But when i use this hook I see this error: Uncaught FirebaseError: Invalid Query.但是当我使用这个钩子时,我看到了这个错误: Uncaught FirebaseError: Invalid Query。 A non-empty array is required for 'in' filters.
“in”过滤器需要一个非空数组。 This is happening beacause new game has 0 connected players.
这是因为新游戏有 0 个连接的玩家。
So the question is - How can I conditionally execute hook depending on the array length?所以问题是 -如何根据数组长度有条件地执行 hook?
The solution was quite simple.解决方案非常简单。 The thing i didn't get is that I can pass null instead of querying collection when no players are connected or players array doesn't exists.
我没有得到的是,当没有玩家连接或玩家数组不存在时,我可以传递 null 而不是查询集合。
export const useGamePlayers = () => {
const {game} = useTypedSelector(state => state.game);
return useCollection(
game?.players.length
? query(collection(db, 'players'), where(documentId(), 'in', game.players))
: null
)
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.