本文介绍了npm react-native-fetch-blob - “RNFetchBlob.fetch 不是函数"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 npm 包 react-native-fetch-blob.
我已按照
git 存储库 中的所有步骤来使用该软件包.

I am using the npm package react-native-fetch-blob.
I have followed all the steps from the git repository to use the package.

然后我使用以下行导入包:

I then imported the package using the following line:

var RNFetchBlob = require('react-native-fetch-blob');

我正在尝试从服务器请求包含图像的 BLOB.

I am trying to request a BLOB containing an image from the a server.

这是我的主要方法.

fetchAttachment: function(attachment_uri) {

   var authToken = 'youWillNeverGetThis!'
   var deviceId = '123';
   var xAuthToken = deviceId+'#'+authToken

   //Authorization : 'Bearer access-token...',
   // send http request in a new thread (using native code)
   RNFetchBlob.fetch('GET', config.apiRoot+'/app/'+attachment_uri, {

       'Origin': 'http://10.0.1.23:8081',
       'X-AuthToken': xAuthToken
    })
    // when response status code is 200
    .then((res) => {
       // the conversion is done in native code
       let base64Str = res.base64()
       // the following conversions are done in js, it's SYNC
       let text = res.text()
       let json = res.json()
    })
    // Status code is not 200
    .catch((errorMessage, statusCode) => {
       // error handling
    });
}

我不断收到以下错误:

可能未处理的 Promise Refection(id: 0): TypeError: RNFetchBlob.fetch is not a function".

有什么想法吗?

推荐答案

问题是您正在使用 ES5 风格的 require 语句和针对 ES6/ES2015 编写的库.你有两个选择:

The issue is you are using ES5 style require statements with a library written against ES6/ES2015. You have two options:

ES5:

var RNFetchBlob = require('react-native-fetch-blob').default

ES6:

import RNFetchBlob from 'react-native-fetch-blob'

这篇关于npm react-native-fetch-blob - “RNFetchBlob.fetch 不是函数"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 14:44