【发布时间】:2016-11-22 06:41:07
【问题描述】:
我正在使用 npm 包react-native-fetch-blob。
我已按照git repository 中的所有步骤使用该软件包。
然后我使用以下行导入包:
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
});
}
我不断收到以下错误:
“可能未处理的 Promise Refection(id: 0): TypeError: RNFetchBlob.fetch is not a function”。
有什么想法吗?
【问题讨论】:
-
试试 var RNFetchBlob = require('react-native-fetch-blob').default;
-
@Cherniv 根据the manual,它应该是
import RNFetchBlob from ...。这就是可能需要.default的原因。 -
@rmevans9 那行得通。把它写成答案,我会接受这个解决方案。谢谢。
标签: node.js npm react-native fetch