【发布时间】:2022-03-04 10:19:51
【问题描述】:
路线:
router.post('/MOCReport', upload.array('files'), MOCReportController.MOCReport_post);
控制器:
exports.MOCReport_post = (req, res, next) => { const files = req.files;
console.log(files.filename);
if(!files){
const error = new Error('Please upload an image')
error.httpStatusCode = 400
return next (error)
}
var paths = req.files.map(files => file.path)
let newMOCReport = new MOCReport({
facilityName: req.body.facilityName,
MoCDescription: req.body.MoCDescription,
MoCReportDateTime: req.body.MoCReportDateTime,
MoCDisasterLocation: req.body.MoCDisasterLocation,
mocImage: req.files.paths
});
newMOCReport.save().then((MOCReportDoc) => {
res.send(MOCReportDoc);
});
};
Schema:
mocImage:{
type: Array,
required: false
},
使用 multer 将多个图像上传到 mongoDB 数据库时出现未定义错误。
错误: 监听 3000 端口 连接MongoDB成功:)
未定义
POST /MoCReport 500 44.414 毫秒 - 43
【问题讨论】:
-
请发布您遇到的确切错误。
-
终端刚刚显示“未定义”,邮递员显示:文件未定义
-
那听起来不像是错误。听起来你记录了一些东西,记录的项目的内容是
undefined。 -
我不明白,它适用于单个图像上传,但是当更改为 upload.array 时它没有
标签: javascript node.js express mongoose multer