Search code examples
node.jsnpmreact-nativefetch

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


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?


Solution

  • 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'