【发布时间】:2021-08-27 07:24:09
【问题描述】:
我的 express 应用程序有一个非常奇怪的问题,我只是想访问通过表单数据通过 post 请求发送的 req.body 数据,但不幸的是,当我尝试访问 request.body 中的这些值时出现未定义的错误但奇怪的是,如果我使用 multer 中间件(我在另一条路线上使用它来上传文件)我没有收到这个错误。 我已经配置了 express 提供的默认正文解析器。
//body pharser
app.use(express.json());
app.use(
express.urlencoded({
extended: true,
})
);
//multer configuration
const ImageUpload = multer({
storage: storage,
limits: { fileSize: 4194304 },
fileFilter: Imagfilter,
});
//this will return undefined
app.post("/available",(req, res) => {
console.log(req.body.name);
}
//but this will return the value without any issues
app.post(
"/available",
ImageUpload.fields([
{ name: "nicImageFront", maxCount: 1 },
{ name: "nicImageBack", maxCount: 1 },
]),
(req, res) => {
console.log(req.body.name);
}
【问题讨论】:
标签: javascript express multer