Multer应该将文件数据放在req.file中,但是req.file是未定义的。
//routes.js
import candidatesRouter from './candidates';
export default function routes(app) {
app.use('/v1/candidates', candidatesRouter);
}
//candidates.js
const multer = require('multer');
const upload = multer({ dest: 'uploads/' });
export default express
.Router()
.post('/:userId/documents/:objectKey', upload.any(), (req, res, next) => {
console.log(req.body); // undefined
console.log(req.file); // undefined
console.log(req.files); // []
});
有人有什么想法吗?谢谢!
答案 0 :(得分:0)
尝试一下
const fileUpload = require('express-fileupload');
app.use(fileUpload());
然后再次检查需求文件
答案 1 :(得分:0)
问题似乎是因为我正在使用Swagger中间件。 https://apidevtools.org/swagger-express-middleware/docs/middleware/parseRequest.html Swagger中间件已经处理了表单数据解析,并默认将其存储在req.files中。