req.query returns nothing


Trying to get variable from http://localhost:3000/categories/subcategories?category_id=13 but req.query returns empty result.

app.get('/subcategories', (req, res) => {
 let category_id = req.query.category_id
 db.query('SELECT subcategory_name FROM subcategories WHERE category_id=(?)',
 (err, result) =>{

Is this possibly a problem with the way I’ve set up my server?


See how you are passing the data , directly entering the data or from another URL you are passing the data.
Ensuring you are not sending data via req.body. Ref :

Empty req.query on ExpressJS

or check res.send({category_id}) . Try printing res.send({"category_id": category_id}) . or see console.log(JSON.stringify(req.query))

Or Extract Query Parameters Without Express

const url = require('url');
const querystring = require('querystring');

let rawUrl = '';

let parsedUrl = url.parse(rawUrl);
let parsedQs = querystring.parse(parsedUrl.query);

Answered By – Senthil

Answer Checked By – David Goodson (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.