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

React Native中Firestore获取数据报错:undefined is not a function 求助

解决方案:Firestore collectionGet 未定义错误及代码问题修正

核心错误:TypeError: undefined is not a function (near '...this._firestore.native.collectionGet...')

该错误表明Firestore的原生桥接模块未正确加载,以下是针对性排查步骤:

1. 确认Firestore模块的正确导入

确保apiGetProductCat函数中使用单独导入的Firestore模块,而非从@react-native-firebase/app间接导入:

import firestore from '@react-native-firebase/firestore';

export function apiGetProductCat() {
  // 原有逻辑保持不变
}

2. 重新构建Expo原生项目

即使Auth功能正常,Firestore模块可能未被正确链接,执行以下操作:

  • 运行 expo prebuild 重新生成原生项目文件
  • 清理缓存后启动项目:npx expo start --clear

3. 验证版本兼容性

Expo 47对应RN 0.70.5,@react-native-firebase 17.x存在潜在兼容风险,尝试降级到适配Expo 47的稳定版本:
修改package.json中的依赖:

"@react-native-firebase/app": "^16.5.0",
"@react-native-firebase/auth": "^16.5.0",
"@react-native-firebase/firestore": "^16.5.0"

执行 npm install 或 yarn install 后重启项目。


代码中的其他错误修正(需同步处理)

1. Redux AsyncThunk 未返回Promise

原代码中未返回apiGetProductCat()的Promise,导致Thunk无法正确处理异步结果:

export const fetchProductCatList = createAsyncThunk(
    "productCat/fetchProductCatList",
    (param) => {
        // 修正:返回异步请求的Promise
        return apiGetProductCat()
            .then(e => e)
            .catch(err => err)
    }
);

2. useMemo 误用异步函数

useMemo的回调不能是异步函数(会返回Promise而非预期值),改用useEffect:

useEffect(() => {
    const fetchData = async () => {
        await props.dispatch(fetchProductCatList())
        setLoaderScreen(false)
    }
    fetchData()
},[])

3. ExtraReducers 名称不匹配

原代码中Thunk名称为fetchProductCatList,但extraReducers中误用了fetchProductCategoriesList,修正后:

extraReducers: (builder) => {
    builder.addCase(fetchProductCatList.fulfilled, (state, action) => {
        console.log('extraReducers fetchProductCatList action.payload', action)
    })
},

内容的提问来源于stack exchange,提问作者Seb

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 13:35:19