1

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?

3
  • 3
    Try var RNFetchBlob = require('react-native-fetch-blob').default; Commented Jul 19, 2016 at 11:44
  • 1
    @Cherniv it should be import RNFetchBlob from ... according to the manual. That's why .default is probably required. Commented Jul 19, 2016 at 12:04
  • @rmevans9 That worked. Write it up as an answer and I will accept the solution. Thanks. Commented Jul 19, 2016 at 12:52

2 Answers 2

5

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'
Sign up to request clarification or add additional context in comments.

Comments

1

My import looks like this : import RNFetchBlob from 'rn-fetch-blob';

but I'v got an error : TypeError: RNFetchBlob.scanFile is not a function

My code:

const downloadAudio = async () => {
    const { config, fs } = RNFetchBlob;
    const meditationFilesPath =
      Platform.OS == 'android'
        ? `${fs.dirs.DownloadDir}/meditations/${id}`
        : `${fs.dirs.DocumentDir}/meditations/${id}`;
    let audio_URL = track;
    let options = {
      fileCache: true,
      path: meditationFilesPath + `/${id}.mp3`,
      addAndroidDownloads: {
        // Related to the Android only
        useDownloadManager: true,
        notification: true,
        path: meditationFilesPath + `/${id}.mp3`,
        description: 'Audio',
      },
    };
    try {
      const resAudio = await config(options).fetch('GET', audio_URL.uri);
      if (resAudio) {
        const audio = await RNFetchBlob.fs.scanFile([
          { path: resAudio.path(), mime: 'audio/mpeg' },
        ]);
        console.log('res -> ', audio);
        Alert.alert('Audio Downloaded Successfully.');
      }
    } catch (error) {
      console.error('error from downloadAudio', error);
    }
  };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.