简体   繁体   English

为什么 express (node.js) 需要参数来删除请求?

[英]Why does express (node.js) require params for delete request?

I made todo react app where user clicks on delete button it fetches delete request to my node.js server.我制作了 todo react 应用程序,用户点击删除按钮,它将删除请求发送到我的 node.js 服务器。

const deleteTodo = (id) => {
  const q = `DELETE FROM todos WHERE id=${id}`;
  connection.query(q, (err, results) => {
    if (err) throw err;
    console.log('delete', results)
  })
}

However, when I simply went with router option below,但是,当我简单地使用下面的路由器选项时,

app.delete('/', function (req, res) {
  (some function) 
})

app wouldn't delete just like this.应用程序不会像这样删除。

On the other hand, by simply making it另一方面,通过简单地制作它

app.delete('/todos/:id', function (req, res) {
  (some function) 
})

it will start functioning again.它将重新开始运作。

Why is this so?为什么会这样?

I think your question has some missing information.我认为您的问题缺少一些信息。
In particular, how you're calling deleteTodo and what the error actually is.特别是,您如何调用deleteTodo以及实际错误是什么。

If I were to make a guess, when /todos/:id is specified, it becomes possible to use req.params.id .如果我猜测一下,当指定/todos/:id ,就可以使用req.params.id
However, when it's / , req.params.id is undefined.但是,当它是/req.params.id是未定义的。 When req.params.id is undefined, the query becomes invalid.req.params.id未定义时,查询无效。

Refer to express documentation for more details.有关更多详细信息,请参阅express 文档

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

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