2016-07-19 45 views
1

我使用的是npm包react-native-fetch-blob
我已遵循git repository的所有步骤来使用该软件包。npm react-native-fetch-blob - “RNFetchBlob.fetch不是函数”

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

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

我试图请求来自服务器包含图像的BLOB。

这是我的主要方法。

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

我不断收到以下错误:

“可能未处理无极便餐(ID:0):类型错误:RNFetchBlob.fetch不是一个函数”。

有什么建议吗?

+3

尝试VAR RNFetchBlob =需要( '反应原生取-斑点')违约; – rmevans9

+1

@Cherniv根据[手册](https://www.npmjs.com/package/react-native-fetch-blob#recipes),它应该是'从...导入RNFetchBlob'。这就是为什么'.default'可能是必需的。 – robertklep

+0

@ rmevans9工作。把它写成答案,我会接受解决方案。谢谢。 – Larney

回答

4

问题是,您正在使用针对ES6/ES2015编写的库的ES5样式require语句。你有两个选择:

ES5:

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

ES6:

import RNFetchBlob from 'react-native-fetch-blob' 
相关问题