[英]How to pass variable to GET request after receiving it from POST request
Can't figure out how to pass a variable to GET request after receiving it from the frontend.从前端接收到变量后,无法弄清楚如何将变量传递给 GET 请求。
app.post('/subcategories', (req, res) => {
//receive category id from front
const category_id = req.body.category;
})
app.get('/subcategories', (req, res) => {
// need to replace the 25 with the variable I recieved from the above POST request
const category_id = 25;
db.query('SELECT subcategory_name FROM subcategories WHERE category_id=(?)',
[category_id],
(err, result) =>{
res.send({result})
});
})
You can directly pass the category_id to GET using query parameters.您可以使用查询参数直接将 category_id 传递给 GET。
Do something like:做类似的事情:
/subcategories?category_id=25
catch it in the GET api using:使用以下命令在 GET api 中捕获它:
let category_id = req.query.category_id
you can pass category_id
as a query when making a get request and grab the category_id
in the '/subcategories'
route using req.query
.您可以在发出获取请求时将
category_id
作为查询传递,并使用req.query
在'/subcategories'
路由中获取category_id
。
make get requests like this.像这样获取请求。
/subcategories?categoryid=25
get it in a query object like this.像这样在查询 object 中获取它。
let categoryId = req.query.categoryid
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.