下面是我的代码和文件结构
在/comments页面中,当您单击详细信息链接时,它会转到编辑页面,然后当您单击编辑注释时,它会将您带到编辑页面,您可以编辑注释,但一旦您单击保存,它就会返回到/comment,但编辑注释和原始注释会消失。不确定代码中的逻辑出了什么问题?
node_modules
views/comments
edit.ejs
index.ejs
new.ejs
show.ejs
index.js
package-lock.json
package.jsonpackage.json
{
"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
<!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
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")
})发布于 2021-05-25 04:23:01
我认为您应该使用app.put()而不是app.patch()
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');
})
https://stackoverflow.com/questions/67678288
复制相似问题