簡體   English   中英

Express.js 刪除請求

[英]Express.js delete request

制作 MERN 堆棧應用程序,但刪除請求 function 不起作用。
這是相關代碼

嘗試使用 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;

router.delete('/:id', (req, res)需要一個參數 id 所以真正的鏈接應該像 DELETE /api/todos/{id},例如 /api/todos/3

正如我注意到您的請求被發送到 /api/todos 沒有參數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM