在前端上传图像时无法读取未定义的属性"路径"(反应)



我试图在前端上传图像,但这不起作用,我在下面收到错误

message: "Cannot read property 'path' of undefined"
status: "fail"

当我在后端记录req.file并尝试在前端上传时,我在控制台中得到了未定义,但这是为req.body 记录的

[Object: null prototype] {
name: 'sfdgg',
description: 'dsfdgg',
reviewImage: '[object Object]' }

通过Postman在后端上传图像效果良好。

这是前端逻辑

const formData = new FormData();
for (let key in review) {
formData.append(key, review[key]);
}
formData.append("reviewImage", reviewImage)
console.log(reviewImage)
axios.post("http://localhost:3001/api/v1/reviews", formData,{
headers: {
"content-type": "multipart/formdata"
}
})

删除内容类型不起作用,因为它也不适用于内容类型。

Multer配置

const upload = multer({
storage: storage,
limits: {
fileSize: 1024 * 1024 * 5,
},
fileFilter: fileFilter,
});
upload.single("reviewImage");

看到类似的问题,但答案对我不起作用,请帮忙。

尝试在表单数据中添加文件

var formData = new FormData();
var imagefile = document.querySelector('#reviewImage');
formData.append("reviewImage", imagefile.files[0]);
axios.post("http://localhost:3001/api/v1/reviews", formData, {
headers: {
"content-type": "multipart/formdata"
}
})

最新更新