const router = express.Router({mergeParams:true});
const wrapAsync = (fn)=>{
return (req,res,next)=>{
fn(req,res,next).catch(next);
}
}
const validateReview = (req,res, next)=>{
let {error} = reviewSchema.validate(req.body);
if(error){
let errMsg = error.details.map((el)=>el.message).join(",");
throw new ExpressError(400, errMsg);
}else{
next();
}
}
router.post("/", validateReview, wrapAsync(async(req,res)=>{
let listing = await Listing.findById(req.params.id);
let newReview = new Review(req.body.review);
listing.reviews.push(newReview);
await newReview.save();
await listing.save();
res.redirect(`/listings/${listing._id}`);
}));
This is the child express route to app.js
Listing validation failed: title: Path `title` is required.
now reviews contains only two fields, rating and comment, and has no title, I don’t understand where I can disable the title requirement from. The values in the form is not validating.
When I try to print the listing, req.params.id, req.body, newReview, its all working. There’s something wrong with validation requirement, which says there should be a valid title, but there’s no title field defined in the reviews object.
Priya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.