Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //CREATE CAMPGROUND ROUTE - add new campground to database.
- router.post("/", middleware.isLoggedIn, function(req, res){
- var name = req.body.name;
- var price = req.body.price;
- var image = req.body.image;
- var description = req.body.description;
- var author = {
- id: req.user._id,
- username: req.user.username
- };
- var newCampground = {name: name, price: price, image: image, description: description, author: author};
- //Create a new campground and save to database.
- Campground.create(newCampground, function(err, campground){
- if(err){
- req.flash("error", "There was an error adding your campground to the database: " + err.message);
- res.redirect("/campgrounds");
- } else {
- req.flash("success", "Your campground has been successfully added!");
- res.redirect("/campgrounds");
- }
- });
- });
- //CREATE COMMENTS ROUTE
- router.post("/", middleware.isLoggedIn, function(req, res){
- Campground.findById(req.params.id, function(err, campground){
- if(err){
- req.flash("error", "There was an error finding the campground you wish to comment on in the database: " + err.message);
- res.redirect("/campgrounds");
- } else {
- Comments.create(req.body.comment, function(err, comment){
- if(err){
- req.flash("error", "There was an error adding your comment to the database: " + err.message);
- res.redirect("back");
- } else {
- //Add user ID and username to comment.
- comment.author.id = req.user._id;
- comment.author.username = req.user.username;
- //Save the comment with user ID and username.
- comment.save();
- //Add comment to the campground.
- campground.comments.push(comment._id);
- campground.save();
- req.flash("success", "Your comment has been successfully added!");
- res.redirect("/campgrounds/" + campground._id);
- }
- });
- }
- });
- });
Add Comment
Please, Sign In to add comment