首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当用户点击编辑评论时,我正在尝试更新用户评论,当用户点击编辑评论时,评论就消失了

当用户点击编辑评论时,我正在尝试更新用户评论,当用户点击编辑评论时,评论就消失了
EN

Stack Overflow用户
提问于 2021-05-25 04:02:00
回答 1查看 23关注 0票数 0

下面是我的代码和文件结构

在/comments页面中,当您单击详细信息链接时,它会转到编辑页面,然后当您单击编辑注释时,它会将您带到编辑页面,您可以编辑注释,但一旦您单击保存,它就会返回到/comment,但编辑注释和原始注释会消失。不确定代码中的逻辑出了什么问题?

代码语言:javascript
复制
node_modules
views/comments
  edit.ejs
  index.ejs
  new.ejs
  show.ejs
index.js
package-lock.json
package.json

package.json

代码语言:javascript
复制
{
  "name": "practicerest",
  "version": "1.0.0",
  "description": "Practice ",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Manny Verma",
  "license": "ISC",
  "dependencies": {
    "ejs": "^3.1.6",
    "express": "^4.17.1",
    "method-override": "^3.0.0",
    "uuid": "^8.3.2"
  }
}

edit.ejs

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit</title>
</head>

<body>
    <h1>Edit</h1>
    <form method="POST" action="/comments/<%=comment.id%>?_method=PATCH">
        <textarea name="listing" id="" cols="30" rows="10"><%= comment.comment %> </textarea>
        <button>Save</button>
    </form>
</body>

</html>

index.js

代码语言:javascript
复制
const { urlencoded } = require('express');
const express = require('express');
const app = express();
const path = require('path');
const { v4: uuid } = require('uuid');
const methodOverride = require('method-override');

app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(methodOverride('_method'))
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

let comments = [{
        id: uuid(),
        username: 'Todd Barkley',
        comment: 'Alright movie!!!!'
    },
    {
        id: uuid(),
        username: 'David Smith',
        comment: 'Oh my that was a weird movie'
    },
    {
        id: uuid(),
        User: 'Erick Jones',
        comment: 'Terrible movie, I walked out half way through it.'
    }
]

//comments
app.get('/comments', (req, res) => {
    res.render('comments/index', { comments });
})

//new
app.get('/comments/new', (req, res) => {
    res.render('comments/new');
})

app.post('/comments', (req, res) => {
    const { username, comment } = req.body;
    comments.push({ username, comment, id: uuid() })
    res.redirect('/comments');
})

//show
app.get('/comments/:id', (req, res) => {
    const { id } = req.params;
    const comment = comments.find(l => l.id === id);
    res.render('comments/show', { comment });
})

//Edit
app.get('/comments/:id/edit', (req, res) => {
    const { id } = req.params;
    const comment = comments.find(l => l.id === id);
    res.render('comments/edit', { comment });
})

// updating
app.patch('/comments/:id', (req, res) => {
    const { id } = req.params;
    const newCommentText = req.body.comment;
    const foundComment = comments.find(l => l.id === id);
    foundComment.comment = newCommentText;
    res.redirect('/comments');
})

//delete
app.delete('/comments/:id', (req, res) => {
    const { id } = req.params;
    comments = comments.filter(l => l.id !== id);
    // filter is a boolean function, for whatever the call back returns true for those elements will be added to the filter array, you want to try to not mutate an array you should make a copy and make a change to the copy of the array that's what comment.filter is doing, it's returning a new array
    res.redirect('/comments');
})

app.listen(4000, () => {
    console.log("On port 4000")
})
EN

回答 1

Stack Overflow用户

发布于 2021-05-25 04:23:01

我认为您应该使用app.put()而不是app.patch()

代码语言:javascript
复制
app.put('/comments/:id', (req, res) => {
    const { id } = req.params;
    const newCommentText = req.body.comment;
    const foundComment = comments.find(l => l.id === id);
    foundComment.comment = newCommentText;
    res.redirect('/comments');
})

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67678288

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档