when res.body is undefined through the query, it is because body-parser is not loaded, but I do not have such a problem, but I also return undefined. What the heck is it?
//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);
when I pass the postman test, the value in form is available:
postman
ask for advice! Thank you!