制作MERN-stack应用程序,但删除请求功能不起作用。
这是相关代码
尝试使用邮递员发送删除请求时,显示此错误。
我搜索其他一些StackOverflow问题,但找不到答案。
在我以前的快速应用程序中,它就像一种魅力。
无法DELTE / api / todos
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<!--This is the error -->
<pre>Cannot DELETE /api/todos</pre>
<!--This is the error ^ -->
</body>
</html>
Todos.js
const express = require('express');
const uuid = require('uuid');
const router = express.Router();
const todos = require('../../Todo');
router.get('/', (req, res) => {
res.json(todos);
});
router.get('/:id', (req, res) => {
const found = todos.some(todo => todo.id === req.params.id);
if (!found) {
res.status(400).json({ msg: `No meber whit id of ${req.params.id}` });
} else {
res.json(todos.filter(todo => todo.id === req.params.id));
}
});
router.post('/', (req, res) => {
const newEntry = {
id: uuid.v4(),
title: req.body.title,
completed: false,
};
if (!req.body.title) {
res.status(400).json({ msg: `Pleas include title` });
}
todos.push(newEntry);
res.json(todos);
});
router.delete('/:id', (req, res) => {
const found = todos.some(todo => todo.id === req.params.id);
if (!found) {
res.status(400).json({ msg: `No meber whit id of ${req.params.id}` });
} else {
todos.filter(todo => todo.id !== req.params.id);
res.json(todos);
}
});
module.exports = router;
最佳答案
router.delete('/:id', (req, res)
需要一个参数id,因此实际链接应类似于DELETE / api / todos / {id},例如/ api / todos / 3
如我所知,您的请求没有参数就发送到/ api / todos
关于javascript - (已修复)Express.js删除请求,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61526572/