React Native+Firebase Storage报错:Cannot read property 'includes' of undefined
问题描述
使用React Native + Expo + Firebase开发应用,实现图片上传至Firebase Storage功能时,调用uploadBytes函数报错:Cannot read property 'includes' of undefined。storage变量已在其他文件通过getStorage(app)完成初始化,且调用listAll读取文件时也出现相同错误。
相关代码:
console.log('Load Image!') console.log('blob !') const storageRef = ref(storage, '/profil'); console.log('upload !') // 'file' comes from the Blob or File API const bytes = new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21]); uploadBytes(storageRef, bytes,'') .then(() => { console.log('Uploaded an array!'); })
解决思路
- 对齐Firebase依赖版本:React Native/Expo环境下,SDK版本不匹配易引发底层逻辑错误。确保
firebase、@firebase/storage等相关包版本一致,且适配当前Expo SDK版本(比如Expo 49+建议搭配Firebase 9.x/10.x模块化版本,避免混用命名空间写法)。 - 验证storage实例有效性:在调用
ref方法前打印storage变量,确认其为合法的Storage实例而非undefined/null。比如添加console.log('storage instance:', storage),排查是否存在导出或初始化环节的疏漏。 - 移除非法参数:
uploadBytes的第三个参数为可选的元数据对象(UploadMetadata类型),传入空字符串不符合参数要求,会触发内部逻辑异常。直接调用uploadBytes(storageRef, bytes)即可,若需配置元数据,传入合法对象(如{ contentType: 'text/plain' })。 - 确保Firebase初始化完成:在React Native/Expo中,需等待Firebase App实例完全初始化后再调用Storage方法。建议在App根组件的
useEffect中完成初始化,避免在实例未就绪时执行操作。
内容的提问来源于stack exchange,提问作者Florian Dordain
相关产品推荐
相关产品推荐

