无法使用此代码编辑评论

时间:2023-01-23 20:34:20

//Comment edit route

//评论编辑路线

router.get("/campgrounds/:id/comments/:comment_id/edit", function(req,res){
Comment.findById(req.params.comment_id  , function(err,foundComment){
    if(err){
        res.redirect("back");
    } else{
        res.render("comments/edit.ejs",{campground_id :req.params.id , 
       comment:foundComment});
          }
});
});

//Comment update route

//评论更新路线

router.put("/campgrounds/:id/comments/:comment_id",function(req,res){
Comment.findByIdAndUpdate( req.params.comment_id, req.body.comment , 
   function(err,updatedComment){
    if(err){
        res.redirect("back");
    }else{

        res.redirect("/campgrounds/" + req.params.id); 
     }

The ejs file is

ejs文件是

 <form action="/campgrounds/<%= campground_id %>/comments/<%=comment._id%>?
    _method=PUT" method="POST"> 
  <div class="container">    
        <input type="text" name="comment[text]" value="<%=comment.text%>">
        <button>Submit</button>
  </div>
</form>

I am not able to edit the comments and update it either, Thanks in advance :)

我无法编辑评论并更新它,在此先感谢:)

And here is the comment model code

这是评论模型代码

 var mongoose= require("mongoose");
 var commentSchema=new mongoose.Schema(
 { text: String,
 author: { id: { type:mongoose.Schema.Types.ObjectId, ref: "User" },
 username: String }
 });
 module.exports=mongoose.model("Comment",commentSchema);

1 个解决方案

#1


0  

I think you need to add some spaces from:

我想你需要添加一些空格:

<%=comment.text%>

to

<%= comment.text %>

#1


0  

I think you need to add some spaces from:

我想你需要添加一些空格:

<%=comment.text%>

to

<%= comment.text %>