undefined is not an object (evaluating 'RNFetchBlob.DocumentDir')

参考https://github.com/wkh237/react-native-fetch-blob/issues/51 自己做了一下总结:

undefined is not an object (evaluating 'RNFetchBlob.DocumentDir')

这个报错位置在react-native-fetch-blob下fs.js:

DocumentDir :  RNFetchBlob.DocumentDir 

undefined is not an object (evaluating 'RNFetchBlob.DocumentDir')

首先看一下最外层package.json下是否有“react-native-fetch-blob”该依赖包

如果没有

就执行下面命令行:

$ npm install --save react-native-fetch-blob@0.10.5

如果react-native-fetch-blob版本较低,移除原有包
$ rnpm uninstall react-native-fetch-blob
重新按上面安装
react-native版本< 0.29 
$ rnpm link
react-native版本 0.29.2+
$ react-native link

执行$ rnpm link或rnpm uninstall时可能提示:

undefined is not an object (evaluating 'RNFetchBlob.DocumentDir')

参考 https://github.com/rnpm/rnpm 执行命令行:

$ npm install rnpm -g

再执行$ rnpm link或rnpm uninstall

如果出现以下提示:

undefined is not an object (evaluating 'RNFetchBlob.DocumentDir')

可参考react-native-fetch-blob\README.md文件:

react-native版本< 0.29

$ RNFB_ANDROID_PERMISSIONS=true rnpm link

react-native版本 0.29.2+
$ RNFB_ANDROID_PERMISSIONS=true react-native link
自动添加权限(亲测RNFB_ANDROID_PERMISSIONS不是内部或外部命令,未解决)。或者用以下手动添加:

对于5.0及以下的设备,你必须手动添加以下权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
对Android 6.0 (API level 23),请求权限参照
https://facebook.github.io/react-native/docs/permissionsandroid.html

如果使用`Android Download Manager`需添加

<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
</intent-filter>

连接成功后,在terminal会出现下面提示

undefined is not an object (evaluating 'RNFetchBlob.DocumentDir')

在settings.gradle中添加

 include ':react-native-fetch-blob'
project(':react-native-fetch-blob').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-fetch-blob/android')

如果node_modules在工作空间根目录,../省略

在build.gradle

 dependencies {
compile project(':react-native-fetch-blob') }

在MainApplication或Activity添加包

import com.RNFetchBlob.RNFetchBlobPackage;

```
高版本下
 @Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new RNFetchBlobPackage(),
new MainReactPackage()
);
}

低版本下

 mReactRootView = new ReactRootView(this);
mReactInstanceManager = ReactInstanceManager.builder()
.addPackage(new RNFetchBlobPackage())
.build();
mReactRootView.startReactApplication(mReactInstanceManager, "Task", null);
setContentView(mReactRootView);

最近在项目中遇到:react-native-cached-image下react-native-fetch-blob报错

undefined is not an object (evaluating 'RNFetchBlob.DocumentDir')

报错位置

undefined is not an object (evaluating 'RNFetchBlob.DocumentDir')

采用rn版本如下:

"react": "^16.0.0-alpha.12",
"react-native": "^0.45.1",
修改react-native-cached-image如下版本后正常
"react-native-cached-image": "^1.2.5"

本文为博主原创文章,请尊重版权,未经博主允许不得转载,转载请注明出处:http://www.cnblogs.com/details-666/p/react-native-fetch-blob.html
执行下面命令或直接运行app
$ react-native run-android
上一篇:for和while循环的区别


下一篇:Java编辑PPT的柱状图,与内嵌的Excel联动