#javascript #node.js #express #ejs
#язык JavaScript #node.js #экспресс #ejs
Вопрос:
Я пытаюсь реализовать функцию комментариев на своем веб-сайте. Мои комментарии успешно отправлены из полей ввода, но не добавляются там, где они должны быть видны.
Комментарии Маршруты
var express = require("express"); var router = express.Router({mergeParams: true}); var Coffeeshop = require("../models/coffeeshop"); var Comment = require("../models/comment"); var middleware = require("../middleware"); // Comments New router.get("/new", middleware.isLoggedIn, function(req, res){ // find coffeeshop by ID Coffeeshop.findById(req.params.id, function(err, coffeeshop){ if(err){ console.log(err); } else { res.render("comments/new", {coffeeshop: coffeeshop}); } }); }); // Comments Create router.post("/", middleware.isLoggedIn, function(req, res){ // look up coffeeshop using ID Coffeeshop.findById(req.params.id, function(err, coffeeshop){ if(err){ console.log(err); res.redirect("/coffeeshops"); } else { // create new comment Comment.create(req.body.comment, function(err, comment){ if(err){ req.flash("error", "Something went wrong"); console.log(err); } else { // add username and id to comment comment.author.id = req.user._id; comment.author.username = req.user.username; // save comment comment.save(); // connect new comment to coffeeshop coffeeshop.comments.push(comment); coffeeshop.save(); req.flash("success", "Successfully added comment"); // redirect coffeeshop show page res.redirect("/coffeeshops/" coffeeshop._id); } }); } }); }); // Comments Edit router.get("/:comment_id/edit", middleware.checkCommentOwnership, function(req, res){ Comment.findById(req.params.comment_id, function(err, foundComment){ if(err){ res.redirect("back"); } else { res.render("comments/edit", {coffeeshop_id: req.params.id, comment: foundComment}); } }); }); // Comments Update router.put("/:comment_id", middleware.checkCommentOwnership, function(req, res){ Comment.findByIdAndUpdate(req.params.comment_id, req.body.comment, function(err, updatedComment){ if(err){ res.redirect("back"); } else { res.redirect("/coffeeshops/" req.params.id); } }); }); // Comments Destroy router.delete("/:comment_id", middleware.checkCommentOwnership, function(req, res){ Comment.findByIdAndRemove(req.params.comment_id, function(err){ if(err){ res.redirect("back"); } else { res.redirect("/coffeeshops/" req.params.id); } }); }); module.exports = router;
Интерфейс, с которого комментарии отправляются через поле ввода.
lt;% include ../partials/header %gt; lt;div class="container"gt; lt;div class="row"gt; lt;h1 style="text-align: center"gt;Add New Comment to lt;%= coffeeshop.name %gt;lt;/h1gt; lt;div style="width: 30%; margin: 25px auto;"gt; lt;form action="/coffeeshops/lt;%= coffeeshop._id %gt;/comments" method="post"gt; lt;div class="form-group"gt; lt;input class="form-control" type="text" name="comment[text]" placeholder="text"gt; lt;/divgt; lt;div class="form-group"gt; lt;button class="btn btn-lg btn-primary btn-block" href="/coffeeshops/lt;%= coffeeshop._id %gt;/comments/"gt;Submit!lt;/buttongt; lt;/divgt; lt;/formgt; lt;a href="/coffeeshops"gt;Go Backlt;/agt; lt;/divgt; lt;/divgt; lt;/divgt; lt;% include ../partials/footer %gt;
Страница, на которой должны быть видны комментарии. Но это не так:
lt;% include ../partials/header %gt; lt;div class="container"gt; lt;div class="row"gt; lt;div class="col-md-3"gt; lt;p class="lead"gt;Coffee Clublt;/pgt; lt;div class="list-group"gt; lt;li class="list-group-item active"gt;Info 1lt;/ligt; lt;li class="list-group-item"gt;Info 2lt;/ligt; lt;li class="list-group-item"gt;Info 3lt;/ligt; lt;/divgt; lt;/divgt; lt;div class="col-md-9"gt; lt;div class="thumbnail"gt; lt;img class="img-responsive" src="lt;%= coffeeshop.image %gt;"gt; lt;div class="caption-full"gt; lt;h4 class="pull-right"gt;$lt;%= coffeeshop.price %gt;/Cuplt;/h4gt; lt;h4gt;lt;agt;lt;%=coffeeshop.name%gt;lt;/agt;lt;/h4gt; lt;pgt;lt;%= coffeeshop.description %gt;lt;/pgt; lt;pgt; lt;emgt;Submitted By lt;%= coffeeshop.author.username %gt;lt;/emgt; lt;/pgt; lt;% if(currentUser amp;amp; coffeeshop.author.id.equals(currentUser._id)){ %gt; lt;a class="btn btn-xs btn-warning" href="/coffeeshops/lt;%= coffeeshop._id %gt;/edit"gt;Editlt;/agt; lt;form id="delete-form" action="/coffeeshops/lt;%= coffeeshop._id %gt;?_method=delete" method="post"gt; lt;button class="btn btn-xs btn-danger"gt;Deletelt;/buttongt; lt;/formgt; lt;% } %gt; lt;/divgt; lt;/divgt; lt;div class="well"gt; lt;div class="text-right"gt; lt;a class="btn btn-success" href="/coffeeshops/lt;%= coffeeshop._id %gt;/comments/new"gt;Add New Commentlt;/agt; lt;/divgt; lt;% coffeeshop.comments.forEach(function(comment){ %gt; lt;div class="row"gt; lt;div class="col-md-12"gt; lt;stronggt;lt;%= comment.author.username %gt;lt;/stronggt; lt;span class="pull-right"gt;2 months agolt;/spangt; lt;pgt; lt;%= comment.text %gt; lt;/pgt; lt;% if(currentUser amp;amp; comment.author.id.equals(currentUser._id)){ %gt; lt;a class="btn btn-xs btn-warning" href="/coffeeshops/lt;%=coffeeshop._id %gt;/comments/lt;%=comment._id %gt;/edit"gt;Editlt;/agt; lt;form id="delete-form" action="/coffeeshops/lt;%=coffeeshop._id %gt;/comments/lt;%=comment._id %gt;?_method=delete" method="post"gt; lt;input type="submit" class="btn btn-xs btn-danger" value="Delete"gt; lt;/formgt; lt;% } %gt; lt;/divgt; lt;/divgt; lt;% }) %gt; lt;/divgt; lt;/divgt; lt;/divgt; lt;/divgt; lt;% include ../partials/footer %gt;