admin管理员组文章数量:1330609
Making a MERN-stack app but the delete request function is not working.
This is the relevant code
When trying to send a delete request with Postman this error shows. I search through some other StackOverflow questions but can't find an answer. In my previous express applications, it has worked like a charm.
Cannot 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,
pleted: 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;
Making a MERN-stack app but the delete request function is not working.
This is the relevant code
When trying to send a delete request with Postman this error shows. I search through some other StackOverflow questions but can't find an answer. In my previous express applications, it has worked like a charm.
Cannot 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,
pleted: 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;
Share
Improve this question
edited Mar 28, 2021 at 15:55
Syscall
19.8k10 gold badges43 silver badges58 bronze badges
asked Apr 30, 2020 at 15:04
Christoffer HennieChristoffer Hennie
791 gold badge4 silver badges10 bronze badges
6
- what's the error? – Zirc Commented Apr 30, 2020 at 15:06
- Please show some logs – Maciek Commented Apr 30, 2020 at 15:07
- Show us how you check it on Postman. – Samim Hakimi Commented Apr 30, 2020 at 15:09
- Is your other request are OK? – Ibrahim Hasnat Commented Apr 30, 2020 at 15:09
- 2 you have no '/' delete route, you have '/:id', so I suppose /api/todos/1 work, while /api/todos doesn't – crystalbit Commented Apr 30, 2020 at 15:11
1 Answer
Reset to default 3router.delete('/:id', (req, res)
requires a parameter id so the real link should be like DELETE /api/todos/{id}, for example /api/todos/3
As I noticed Your request is sent to /api/todos without parameter
本文标签: javascriptExpressjs delete requestStack Overflow
版权声明:本文标题:javascript - Express.js delete request - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742270428a2444199.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论