You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native Firebase报错:Expected type '_h', 实际为FirestoreQuery对象

解决FirebaseError: Expected type '_h', but it was: a custom FirestoreQuery object的建议

以下是针对该错误的具体排查和解决步骤:

  • 确保Query对象为Firestore原生实例
    必须通过Firebase官方API链式创建查询对象,禁止使用自定义封装的Query类型。正确创建示例:

    import { getFirestore, collection, query, where, orderBy } from 'firebase/firestore';
    
    const db = getFirestore();
    const todosCollection = collection(db, 'todos');
    // 按当前用户ID过滤并按创建时间降序排序
    const todosQuery = query(
      todosCollection,
      where('userId', '==', currentUser.uid),
      orderBy('createdAt', 'desc')
    );
    
  • 检查依赖版本兼容性
    该错误多数是react-firebase-hooks与Firebase SDK版本不匹配导致的类型冲突。建议统一升级到最新兼容版本:

    # npm用户
    npm update firebase react-firebase-hooks
    
    # yarn用户
    yarn upgrade firebase react-firebase-hooks
    

    升级后重启项目,验证错误是否消失。

  • 规范useCollectionData的调用方式
    确保useCollectionData的第一个参数是原生Firestore Query对象,而非集合引用或其他类型。调用时建议指定idField参数,方便后续获取文档ID:

    import { useCollectionData } from 'react-firebase-hooks/firestore';
    
    const [todos, loading, error] = useCollectionData(todosQuery, { idField: 'id' });
    
  • 核对API导入路径
    所有Firestore相关方法必须从官方模块化路径导入,禁止混用旧版命名空间API:

    // 正确导入方式
    import { collection, query, where, orderBy, deleteDoc, doc } from 'firebase/firestore';
    // 错误示例:不要混用旧版API
    // import firebase from 'firebase/app';
    
  • 验证删除逻辑中的文档引用
    在handleDelete函数中,必须使用正确的文档引用执行删除操作:

    const handleDelete = async (todoId) => {
      if (confirm('确定删除该待办项?')) {
        const todoDoc = doc(db, 'todos', todoId);
        await deleteDoc(todoDoc);
      }
    };
    

    这里的doc必须通过Firestore实例和文档ID创建,不能传入错误的对象类型。

内容的提问来源于stack exchange,提问作者13 178 Hefin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 22:10:09