我正在将图像上传到Cloudinary,并希望在上传后检索图像url。上传是正确的,我可以在服务器端console.log url,但之后响应不会在客户端的axios调用中处理,也没有出现错误消息。
我的客户:
submitFile = () => {
var formData = new FormData();
formData.append('image', this.state.file, 'tmp_name');//this.state.file is not correct format compared to postman constructed
axios.post('http://localhost:3000/upload', formData).then(function(response) {
console.log(response);
});
}
我的服务器:
server.post('/upload', async (req, res, next) => {
const upload = multer({ storage }).single('image')
upload(req, res, function(err) {
if (err) {
return res.send(err)
}
const path = req.file.path
cloudinary.uploader.upload(
path,
{ public_id: `${filename()}` },
async function(err, image) {
if (err) return res.send(err)
console.log('file uploaded to Cloudinary')
// remove file from server
const fs = require('fs')
fs.unlinkSync(path)
}
).then(result => {res.send(result)})
})
同样,我只希望能够查看响应并保存它,但是之后的所有内容.then都不会执行。
发布于 2019-06-18 15:30:05
您需要删除服务器代码中的括号
server.post('/upload', async (req, res, next) => {
/* upload code*/
).then(result => res.send(result))
})
或在服务器代码中添加返回
server.post('/upload', async (req, res, next) => {
/*upload code*/
).then(result => { return res.send(result)})
})
https://stackoverflow.com/questions/56643321
复制相似问题