Can't figure out how to pass a variable to GET request after receiving it from the frontend.
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.
Do something like :
/subcategories?category_id=25
catch it in the GET api using :
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.
make get requests like this.
/subcategories?categoryid=25
get it in a query object like this.
let categoryId = req.query.categoryid