简体   繁体   English

我正在尝试使用复杂的数组创建放置请求和删除请求

[英]I'm trying to create a put request and delete request with a complicated array

I'm trying to create a put request and delete but I don't know how to do with the DUMMY-PLACES.I will get at the beginning (id, content) and the completed by default is false (it's a dummy DB ) for example (6,test6) of course that's the number in byIds need to be the same"6"我正在尝试创建一个放置请求并删除,但我不知道如何处理 DUMMY-PLACES。我将在开始时获得(id,内容)并且默认完成是 false(它是一个虚拟数据库)例如 (6,test6) 当然这是 byIds 中的数字需要相同“6”

 const express = require("express"); const router = express.Router(); const DUMMY_PLACES = [ { todos: { allIds: [1, 2, 3, 4], byIds: { "1": { content: "test1", completed: false, }, "2": { content: "test2", completed: false, }, "3": { content: "test3\\", completed: false, }, "4": { content: "test4", completed: false, }, }, }, visibilityFilter: "all", }, ]; router.get("/todos", (req, res, next) => { try { const place = DUMMY_PLACES; console.log(place); res.json({ place }); } catch (err) { next({ status: 400, message: "failed to get todos" }); } // => { place } => { place: place } }); router.put("/todos/", async (req, res, next) => { const { id, content } = req.body; try { todo = DUMMY_PLACES; console.log(todo.todos); } catch (err) { next({ status: 400, message: "failed to update todo" }); } }); module.exports = router;

To edit a single todo, first check if it actually exists and update it according.要编辑单个待办事项,首先检查它是否确实存在并相应地更新它。

router.put("/todos", async (req, res, next) => {
  const { id, content } = req.body;
  // Check if todo actually exists
  const todoExists = DUMMY_PLACES[0].todos.byIds.hasOwnProperty(id);
  if (!todoExists) {
    next({ status: 400, message: `todo with id ${id} does not exist` });
  }
  // Update todo
  DUMMY_PLACES[0].todos.byIds[id].content = content;
  res.sendStatus(200);  
});

To remove a todo, delete the key from the byIds object要删除待办事项,请从 byIds object 中删除密钥

router.put("/todos", async (req, res, next) => {
  const { id } = req.body;
  delete DUMMY_PLACES[0].todos.byIds[id];
  res.sendStatus(200);  
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM