Ask coding questions

← Back to all posts
Express req.params always returning empty object
MaxwellGeringer

I am trying to get url parameters through req.params, but it always returns an empty object. I have tried many different solutions, though none have worked.

Answered by ruiwenge2 [earned 5 cycles]
View Answer
Voters
MaxwellGeringer
Comments
hotnewtop
ruiwenge2

using req.params is not the right way to get url parameters. use req.query

MaxwellGeringer

@ruiwenge2 Thanks a lot! For some reason, I read somewhere that something like req.params.foo returns '?foo=bar' and req.params.foo returns 'bar'. Not sure where though.

ruiwenge2

@MaxwellGeringer your welcome!

ruiwenge2

@MaxwellGeringer btw to return ?foo=bar you need to do this:
app.get(“/:query”, ()=>{res.send(req.params.query);});

Coder100

you meant req.query.
Here is the difference:

  • params is for /:whatever/:whatever
  • query is for /?key=value
MaxwellGeringer

@Coder100 Thanks a lot! For some reason, I read somewhere that something like req.params.foo returns '?foo=bar' and req.params.foo returns 'bar'. Not sure where though.

Coder100

hm
you must have read it wrong it is
req.query.foo lol @MaxwellGeringer