服务器上的删除功能删除带有 bunch ID 的数组

Removing function on server which delete array with bunch IDs

我有一个看起来像这样的应用程序picture 1

我选择了复选框,在我按下批量删除后,在状态下,我有我选择的 ID 数组 (picture 2) 我在控制器中的删除功能如下所示

const deleteProduct = async (req, res) => {
  const { id: productId } = req.params;
  const product = await Product.findOne({ _id: productId });
  console.log(product);

  if (!product) {
    throw new Error(`No product with ${productId}`);
  }
  await product.remove();
  res.status(200).json({ msg: "Success, product removed" });
};

但是这个功能只能在 postman 的 route 上使用

router.route("/:id").delete(deleteProduct)

如何编写一个接受项目数组和路线的函数?

首先不清楚你用的是什么方法(POST还是GET)?

获取方法:

URL 例子

my-api/products?array[]=1&array[]=2&array[]=3

然后可以读取array变量如下:

route.get('my-api/products', (req, res) => {
   console.log(req.query.array); // [1, 2, 3]
})

POST方法:

您必须向您的端点发送 POST 请求(例如 axios):

axios.post('my-api/products', {
   selectedItems: [] // your array of ids
})

然后读取ids数组

route.post('my-api/products', (req, res) => {
   console.log(req.body.selectedItems); // [1, 2, 3]
})