React native 将图像上载到服务器时,react本机图像选择器出现错误
今天,我尝试使用React native 将图像上载到服务器时,react本机图像选择器出现错误,react-native,axios,form-data,react-native-image-picker,React Native,Axios,Form Data,React Native Image Picker,今天,我尝试使用react native image picker将手机图像上传到服务器,但遇到了问题。这是我的代码: ImagePicker.showImagePicker(options, (response) => { if (response.didCancel) { console.log('User cancelled image picker'); } else if (response.
react native image picker
将手机图像上传到服务器,但遇到了问题。这是我的代码:
ImagePicker.showImagePicker(options, (response) => {
if (response.didCancel) {
console.log('User cancelled image picker');
} else if (response.error) {
console.log('ImagePicker Error: ', response.error);
} else if (response.customButton) {
console.log('User tapped custom button: ', response.customButton);
} else {
var FormData = require('form-data');
const data = new FormData();
data.append('file', {
uri: `file://${response.path}`,
type: response.type,
name: response.fileName
});
Axios.post('https://api.hoc68.com/api/v1/upload_files', data, {
headers: {
'Authorization': `Bearer ${stateTree.user.token_key}`,
'Content-type': 'multipart/form-data',
}
}).then(res => console.log(res.data)).catch(e => console.log(e.response.data))
console.log(response.path + ' ' + response.type);
}
});
当我运行代码时,我得到了这个错误
可能未处理的承诺拒绝(id:1):TypeError:undefined不是一个对象(评估'e.response.data')
。有人能帮我修复这个错误吗?你的res.data里面有什么?它没有记录res.data,它只捕获错误,当我将它记录到控制台时,它说“未定义请发布你的后端代码,它接受文件数据”。@QuangBìnhĐinh它应该记录一些东西。console.log(“ResData:+JSON.stringify(res.data))在随后的块mabye中尝试此操作。我尝试记录整个错误,它会显示带有堆栈“createError@\nhandleError@”的“网络错误”。堆栈错误很长,我不知道为什么