I want to save the author_id from the req which is not a part of the form instead is one of the variable parameters in URL.
Currently, I am adding the form parameters separately from req.body & author_id from req.user._id & making a new object by combining the above two.
router.post("/", middleware.isLoggedIn, function(req, res){
// get data from form and add to campgrounds array
var name = req.body.name;
var image = req.body.image;
var desc = req.body.description;
var author = { //not a part of req.body but I want to add it in new object
id: req.user._id,
username: req.user.username
}
var price = req.body.price;
var location = req.body.location;
var newCampground = {name: name, image: image, description: desc, price: price, author:author, location: location};
Campground.create(newCampground, function(err, newlyCreated){
if(err){
console.log(err);
} else {
res.redirect("/campgrounds");
}
});
});
I want a shorter format or way to do this because the data from req.body might get bigger in future & I would not want to extract everything separately & combine it with my author key.