在Node.js中使用Multer时未定义req.files

时间:2020-03-23 18:21:23

标签: node.js multer

我正在尝试使用Multer上传混合文件,但在打印[Object: null prototype] {}时遇到req.files错误。

我的路线:

router.post(
  "/t-upload-files",
  upload.fields([
    { name: "passport_image", maxCount: 1 },
    { name: "file", maxCount: 5 }
  ]),
  async (req, res) => { 
    const { user_id } = req.body;
    console.log(req.files) //prints above error
    //...
  }
)

存储:

 const storage = multer.diskStorage({
  destination: (req, file, cb) => {
    const destination = path.join(
      __dirname,
      "/../../uploadedFiles/",
      req.body.user_id.toString()
    );
    fs.mkdirsSync(destination);
    cb(null, destination);
  },
  filename: (req, file, cb) => {
    cb(
      null,
      file.originalname
    );
  }
});
const upload = multer({ storage });

通过邮递员发送文件时,我使用form-data作为正文。

我在做什么错了?

0 个答案:

没有答案