无法使用 Multer 上传多张图片

问题描述

我正在制作一个注册页面,我会在其中要求用户上传 2 张图片,并将它们存储在不同的位置。

我猜这个问题是因为 Maulter。

这是我的路由器

 router.post('/verification',fileUpload.fields([{
               name: 'aadhar',maxCount: 1},{
                name: 'profile_pic',maxCount: 1}]),service_controller_stage1.step2_verification);

这是我的 Multer 文件

const multer = require('multer');


const MIME_TYPE_MAP = {
   'image/png': 'png','image/jpeg': 'jpeg','image/jpg': 'jpg'
};

 const fileUpload = multer({
    limits: 500000,storage: multer.diskStorage({
   destination: (req,file,cb) => {
  if (file.fieldname === "aadhar") { // if uploading resume
    cb(null,'uploads/aadhar');
   } else { // else uploading image
     cb(null,'uploads/DP');
      }
   },filename: (req,cb) => {
  const ext = MIME_TYPE_MAP[file.mimetype];
  cb(null,file.fieldname + '-' + Date.Now());
  }
 }),fileFilter: (req,cb) => {
   const isValid = !!MIME_TYPE_MAP[file.mimetype];
 let error = isValid ? null : new Error('Invalid mime type!');
  cb(error,isValid);
}
});

  module.exports = fileUpload;

这是我的 MongoSchema

const mongoose = require('mongoose');
     const schema =  mongoose.Schema;
      const building_material_schema = new schema({
           image: { type: String,required: true },});

     module.exports = mongoose.model('Building_material',building_material_schema);
This is my Function in the controller



 const step2_verification  = async (req,res,next)=>{
  
    console.log(req.file.path);

   
     };

{控制台只能看到不工作的输出} 我将使用模式在这里保存图像。 我的错误

RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: LIMIT_UNEXPECTED_FILE
    at ServerResponse.writeHead (_http_server.js:253:11)
    at ServerResponse._implicitHeader (_http_server.js:244:8)
    at write_ (_http_outgoing.js:674:9)
    at ServerResponse.end (_http_outgoing.js:787:5)
    at ServerResponse.send (D:\1ST ASSIGNMENT\try\k\backend\node_modules\express\lib\response.js:221:10)
    at ServerResponse.json (D:\1ST ASSIGNMENT\try\k\backend\node_modules\express\lib\response.js:267:15)
    at D:\1ST ASSIGNMENT\try\k\backend\app.js:30:9
    at Layer.handle_error (D:\1ST ASSIGNMENT\try\k\backend\node_modules\express\lib\router\layer.js:71:5)
    at trim_prefix (D:\1ST ASSIGNMENT\try\k\backend\node_modules\express\lib\router\index.js:315:13)
    at D:\1ST ASSIGNMENT\try\k\backend\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (D:\1ST ASSIGNMENT\try\k\backend\node_modules\express\lib\router\index.js:335:12)
    at next (D:\1ST ASSIGNMENT\try\k\backend\node_modules\express\lib\router\index.js:275:10)
    at Layer.handle_error (D:\1ST ASSIGNMENT\try\k\backend\node_modules\express\lib\router\layer.js:67:12)
    at trim_prefix (D:\1ST ASSIGNMENT\try\k\backend\node_modules\express\lib\router\index.js:315:13)
    at D:\1ST ASSIGNMENT\try\k\backend\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (D:\1ST ASSIGNMENT\try\k\backend\node_modules\express\lib\router\index.js:335:12)
`

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)