gpt4 book ai didi

javascript - 如何在nodejs中获取项目的单独id

转载 作者:行者123 更新时间:2023-12-02 21:38:49 25 4
gpt4 key购买 nike

我有一个删除API,它接受我们要删除的项目的ID,每个项目都有其唯一的ID,我如何获取要删除的项目的ID并将其传递给我的API

这里是API

    app.post("/delete_product", function (req, res) {
var data = {
id: req.session.user_id,
token: req.session.token,
product_id: 4
// you need to pass in the id of the product in data.product_id
};
functions.callAPIPost(
"https:/theurl/delete_product",
data,
function (error, result) {
var response = result;
if (response.status === 400) {
console.log('ggg',response)
res.render('products', {
result: result.data
})
} else {
res.redirect('logout')
}
}

);
});

这是我的 ejs

<form method='post'  action='/delete_product'>
<button><iclass="os-icon os-icon-trash" style='font-size: 16px; display: inline-block; vertical-align: middle; margin-right: 10px; color: #8095A0;'></i><span>Delete</span></button>
</form>

我有一个对象数组,在我循环遍历它们后,它们在浏览器中显示为项目,它们都有各自的 id,一旦用户单击调用 api 的项目删除按钮,我如何获取 id删除它。

 [
{
"id": 3,
"name": "work dress",
"description": "take to work dress",
"price": "2000",
"created_at": "2020-02-26T20:30:08.000Z"
},
{
"id": 4,
"name": "movie dress",
"description": "take to movie dress",
"price": "2000",
"created_at": "2020-02-26T20:30:08.000Z"
},
{
"id": 5,
"name": "home dress",
"description": "stay at home dress",
"price": "2000",
"created_at": "2020-02-26T20:30:08.000Z"
}
]

最佳答案

您可以通过 POST URL 传递它,并通过 req.params 访问 NodeJS 中的值。

在 EJS 中 -

<form method='post'action='/delete_product/<%= product._id %>?_method=DELETE'>

在 NodeJS 中 -

app.delete("/delete_product/:id", function (req, res) {

var id_to_delete = req.params.id;

//rest of the code
});

关于javascript - 如何在nodejs中获取项目的单独id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60423840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com