正在制作MERN堆栈应用程序,但删除请求功能不起作用。
这是相关代码
当尝试使用Postman发送删除请求时,显示此错误。 我搜索其他一些StackOverflow问题,但找不到答案。 在我以前的快速应用程序中,它就像一种魅力。
无法删除/ 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;
答案 0 :(得分:1)
router.delete('/:id', (req, res)
需要一个参数ID,因此实际链接应类似于DELETE / api / todos / {id},例如/ api / todos / 3
我注意到您的请求没有参数发送到/ api / todos