admin管理员组

文章数量:1331893

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
 |  Show 1 more ment

1 Answer 1

Reset to default 3

router.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