npm react-native-fetch-blob - “RNFetchBlob.fetch is not a function”

自作多情 提交于 2020-01-24 00:00:49

问题


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');

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

This is my main method.

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
    });
}

I keep receiving the following error:

"Possible Unhandled Promise Refection(id: 0): TypeError: RNFetchBlob.fetch is not a function".

Any ideas?


回答1:


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'


来源:https://stackoverflow.com/questions/38457345/npm-react-native-fetch-blob-rnfetchblob-fetch-is-not-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!