使用 multer 中间件为 req.files 返回未定义的快速路由

时间:2021-03-19 06:19:06

标签: node.js express multer

我是 multer 的新手,当我尝试从邮递员上传图像时,它返回 req.files 的错误未定义,我的错误是什么?
这是我的代码

const multer = require("multer");

const multerStorage = multer.memoryStorage();

const multerFilter = (req, file, cb) => {
    if (file.mimetype.startsWith("image")) {
        cb(null, true);
    } else {
        cb("Please upload only images.", false);
    }
};

const upload = multer({
    storage: multerStorage,
    fileFilter: multerFilter
});

const uploadFiles = upload.array("picture", 3);

const uploadImages = (req, res, next) => {
    console.log("Request file ---"+req.files);//here req.files always return undefined
    console.log("uploadImages section"+JSON.stringify(req.body));


    uploadFiles(req, res, err => {
        if (err instanceof multer.MulterError) {
            if (err.code === "LIMIT_UNEXPECTED_FILE") {
                return res.send("Too many files to upload.");
            }
        } else if (err) {
            console.log(err)
            return res.send(err);
        }

       next();
    });
};

谢谢

0 个答案:

没有答案