提问者:小点点

UnhandledPromiserEjectionWarning:TypeError:赋值给常量变量。at deleteTodo[重复]


我正在尝试执行一个基本的delete todo方法,但没有成功。 我不知道为什么我会出现这个错误,希望得到帮助。 错误:(节点:6416)UnhandledPromiserEjectionWarning:TypeError:向常量变量赋值。 在deleteTodo

代码:

null

const deleteTodo = async (req, res, next) => {
  const { id } = req.body;
  DUMMY_PLACES = DUMMY_PLACES.filter((p) => p.id !== id);
  res.status(200).json({ message: "Deleted " });
};

null

Dummy_DB

null

const DUMMY_PLACES = [
  {
    id: 01,
    todo: "thats my first test to do",
    enable: false,
  },
  {
    id: 02,
    todo: "thats my second test to do",
    enable: false,
  },
  {
    id: 03,
    todo: "thats my third test to do",
    enable: false,
  },
];

null

邮递员中的删除请求:

null

{
    "id": 3
}

null


共2个答案

匿名用户

const禁止将值重新分配给已定义的变量,您可以使用:

DUMMY_PLACES = DUMMY_PLACES.filter((p) => p.id !== id);

如果要执行此类操作,请使用let而不是const,例如:

let DUMMY_PLACES = ...

匿名用户

我相信您正在为节点后端编写这些代码。 则应指定处理delete方法的路由,如:

app.delete('/',async(req,res)=>{//您删除逻辑})

相关问题