Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does req.params return an empty array?

Tags:

I'm using Node.js and I want to see all of the parameters that have been posted to my script. To get to my function, in my routes/index.js I'm doing:

app.post('/v1/order', order.create); 

Then in my function, I have:

exports.create = function(req, res, next) {  console.log( req.params ); 

But it's returning an empty array. But when I do:

exports.create = function(req, res, next) {  console.log( req.param('account_id') ); 

I get data. So I'm a bit confused as to what's going on here.

like image 361
Shamoon Avatar asked May 09 '12 20:05

Shamoon


People also ask

What is req params in Express?

The req. params property is an object that contains the properties which are mapped to the named route "parameters". For example, if you have a route as /api/:name, then the "name" property is available as req.params.name. The default value of this object is {}.

What does request params do?

# req. param() searches the URL path, body, and query string of the request (in that order) for the specified parameter. If no parameter value exists anywhere in the request with the given name , it returns undefined or the optional defaultValue if specified.

What is the type of REQ params?

params is an object of the req object that contains route parameters. If the params are specified when a URL is built, then the req. params object will be populated when the URL is requested.

What are req params returns?

The req. param() function basically returns the value of param name when present. When parameters are passed, then it can be accessed using this function. Parameter: The name parameter is the name of the parameter.


1 Answers

req.params
can only get the param of request url in this pattern:/user/:name

req.query
get query params(name) like /user?name=123 or body params.

like image 171
Eddy Avatar answered Oct 13 '22 16:10

Eddy