简体   繁体   English

错误:“输入”过滤器需要非空数组

[英]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.

相关问题 tokens 必须是 FirebaseMessagingError.FirebaseError 中的非空数组 - tokens must be a non-empty array at FirebaseMessagingError.FirebaseError 文档路径必须是非空字符串,Flutter - Firebase错误? - A document path must be a non-empty string, Flutter - Firebase error? Azure Pipeline Data flow ERROR 不允许在非空目录上执行此操作 - Azure Pipeline Data flow ERROR This operation is not permitted on a non-empty directory AWS CDK S3 部署错误 - 上传的文件必须是非空的 zip - AWS CDK S3 Deployment Error - Uploaded file must be a non-empty zip 上传的文件必须是非空的 zip(服务:AWSLambdaInternal;状态代码:400;错误代码:InvalidParameterValueException; - Uploaded file must be a non-empty zip (Service: AWSLambdaInternal; Status Code: 400; Error Code: InvalidParameterValueException; 删除非空谷歌存储桶的快速方法? - Fast way of deleting non-empty Google bucket? 参数“documentPath”的值不是有效的资源路径。 路径必须是非空字符串 - Value for argument "documentPath" is not a valid resource path. Path must be a non-empty string 我可以强制 CloudFormation 删除非空 S3 存储桶吗? - Can I force CloudFormation to delete non-empty S3 Bucket? Flutter firebase firestore:'path.isNotEmpty':文档路径必须是非空字符串) - Flutter firebase firestore: 'path.isNotEmpty': a document path must be a non-empty string) Firebase 数组为空时出现未定义错误 - Firebase undefined error when array is empty
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM