在Edge浏览器运行React Native时使用react-native-document-picker报TypeError错误
解决React Native中DocumentPicker的TypeError问题
问题根源
- 导入语句错误:当前代码导入的是
pickDirectory,但实际使用的是DocumentPicker对象,直接导致DocumentPicker未定义,进而引发后续的读取属性错误。 - 版本不兼容或安装不完整:
getEnforcing类型错误通常是react-native-document-picker版本与React Native版本不匹配,或者依赖包未正确安装、链接导致的。
修复步骤
1. 修正导入语句
按照react-native-document-picker的正确用法调整导入逻辑:
import DocumentPicker from "react-native-document-picker"; export async function importDocument(fileType, multiSelect) { try { console.log("importDocument") const doc = await DocumentPicker.pick({ type: [DocumentPicker.types[fileType]], allowMultiSelection: multiSelect }); // 按需返回处理结果 return doc; } catch (error) { console.error(`get error ${error} in ImportDocument importDocument`); // 可抛出错误供上层逻辑处理 throw error; } }
2. 检查并适配依赖版本
- 确认
react-native-document-picker版本与你的React Native版本兼容(比如RN 0.70+建议使用最新稳定版)。 - 重新安装依赖包:
npm uninstall react-native-document-picker npm install react-native-document-picker - 若为iOS项目,执行pod安装:
cd ios && pod install && cd ..
3. 清除缓存并重启项目
- 清除Metro缓存:
npx react-native start --reset-cache - 重新运行项目:
npx react-native run-android # 针对Android项目 # 或 npx react-native run-ios # 针对iOS项目
4. 验证权限配置
确保平台权限配置正确:
- Android:在
AndroidManifest.xml中添加权限声明:<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> - iOS:在
Info.plist中添加权限描述:<key>NSDocumentPickerUsageDescription</key> <string>需要访问您的文件以导入文档</string>
内容的提问来源于stack exchange,提问作者tara Narimani
相关产品推荐
相关产品推荐

