express router.post 时,res.body为undefined
通过查询说是 res.body 为 undefined 时,是因为没有加载 body-parser ,但是我不存在这样的问题,却也返回 undefined ,是什么鬼啊?
//server.js 代码不完整,说明 body-parser 已经加载
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({extended:false}))
app.use(bodyParser.json());
app.use(passport.initialize());
const anvizApi = require('./router/api/anviz/home');
app.use('/api/anviz/home',anvizApi);
//router/api/anviz/home.js
//res.body 返回为 undefined
router.post('/banner',passportInfo,(req,res) => {
const {msg,isValid} = homeBannerValidator(req.body);
if(!isValid){
return res.status(400).json(msg);
}
HomeSchema.findOne({banner:req.body._id}).then(banner => {
console.log('current: ' + res.body);
//根据Model 数据模型组织数据
const newBanner = {
handle:req.body.handle,
bannerBg:req.body.bannerBg,
bannerName:req.body.bannerName,
bannerSubName:req.body.bannerSubName,
bannerFeather:req.body.bannerFeather,
bannerLink:req.body.bannerLink
};
banner.prodcutBanner = newBanner;
banner.then(banner => res.json(banner));
})
.catch((err) => res.json(err));
});
module.exports = router;
//Schema
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const HomeSchema = new Schema({
handle:{
type:Boolean
},
prodcutBanner:{
bannerBg:{
type:String,
required:true
},
bannerName:{
type:String,
required:true
},
bannerSubName:{
type:String
},
bannerFeather:{
type:String
},
bannerLink:{
type:String
}
},
prodcutList:[
{
prodctName:{
type: String,
required:true
},
prodctSubTitel:{
type:String,
required:true
},
prodctDes:{
type:String,
required:true
},
prodctLink:{
type:String,
required:true
},
prodctImg:{
type:String,
required:true
},
prodctFeature:[
{
name:{
type:String
}
}
]
}
],
prodctCase:[
{
caseImg:{
type:String,
required:true
},
caseTitle:{
type:String,
required:true
},
caseSubTitle:{
type:String,
required:true
},
caseLink:{
type:String,
required:true
}
}
]
});
module.exports = mongoose.model("home",HomeSchema);
我通过 postman 测试时,form 中的值是可以获取到的:
postman 中返回一个空对象!!!!!
求指点!谢谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的bodyParser有加到use里面吗?
打印一下