javascript - 更新/删除 Node js中的注释

标签 javascript node.js express ejs

当用户点击编辑评论并提交编辑表单时,我正在尝试更新用户的评论。但是当有来自同一个用户的多条评论时,评论更新请求进来,node js 更新该用户的第一条评论。 (我的删除评论操作也有同样的问题)。 有人可以帮助解决这个问题吗?

这是我的ejs代码

<% article.comments.forEach(comment => { %>
            <div class="my-3">
                <strong><%= comment.author.username %></strong>
                <span class="float-right">
                    <%= comment.created.getHours() %>:<%= comment.created.getMinutes() %>
                    <%= comment.created.getMonth() + 1 %>/<%= comment.created.getDate() %>/<%= comment.created.getYear() + 1900 %>
                </span>
                <p><%= comment.content %></p>
                <% if(user && comment.author.id.equals(user._id)) { %>
                    <a  class="btn btn-sm btn-warning float-left mr-2" 
                        onclick="toggleEditForm(`<%= comment._id %>`)">
                        Edit
                    </a>
                    <form id="delete-form" action="/articles/<%= article._id %>/comments/<%= comment._id %>?_method=DELETE" method="POST">
                        <button class="btn btn-sm btn-danger">Delete</button>
                    </form>
                <% } %>
            </div>
            <div id="<%= comment._id %>" style="display: none;">
                <form action="/articles/<%= article._id %>/comments/<%= comment._id %>?_method=PUT" method="POST">
                    <div class="form-group">
                        <label for="inputContent">Edit comment</label>
                        <textarea class="form-control" name="content" id="inputContent" rows="3" placeholder="Type here"><%= comment.content %></textarea>
                    </div>
                    <input type="submit" class="btn btn-primary" value="Edit Comment">
                </form>
            </div>
        <% }) %>

我的 Node js Controller 更新评论和删除评论方法

exports.putUpdateComment = (req, res, next) => {
    const content = req.body.content;
    const author = {
        id: req.user._id,
        username: req.user.username
    };
    const newComment = {
        content: content,
        author: author
    };
    Comment.findOneAndUpdate(req.params.commentId, newComment)
        .then(updatedComment => {
            req.flash('success', 'Your comment was updated');
            res.redirect('/articles/' + req.params.id);
        })
        .catch(err => {
            const error = new Error(err);
            error.httpStatusCode = 500;
            return next(error);
        })
};

exports.deleteComment = (req, res, next) => {
    Comment.findOneAndRemove(req.params.commentId)
        .then(() => {
            req.flash('success', 'Comment was deleted');
            res.redirect('/articles/' + req.params.id);
        })
        .catch(err => {
            const error = new Error(err);
            error.httpStatusCode = 500;
            return next(error);
        })
}

最佳答案

Textarea 字段的 ID 应该是唯一的。

关于javascript - 更新/删除 Node js中的注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59006161/

相关文章:

node.js - 在路由路径中搜索公共(public)文件夹

使用 Node.js/express 服务器进行 ajax GET 调用

javascript - Knockout 无法解析绑定(bind)内部方法

javascript - codeigniter版本如何在不刷新网页的情况下插入并显示记录?

node.js - 使用 webrtc + node.js 进行视频 session

node.js - URL 在浏览器中加载,但不在终端中加载(curl 或 Node.js)

javascript - 按不同的 id 对项目数组进行分组

php - 用于缩小/压缩 javascript 的 php 中的简单正则表达式搜索和替换?

node.js - Docker 主机名未在 next.js prod 中解析但在开发模式下工作(错误 : getaddrinfo ENOTFOUND)

node.js - Namecheap:Node JS Express 应用程序 - 应用程序路由返回 404 未找到