How to get the query parameters in Iron-router?
Asked Answered
S

8

29

I am trying to get the query parameters in the url.

There doesn't seem to be an easy way to do this...

which leaves me with the feeling that I must have missed a something in the doc.

Squally answered 14/4, 2014 at 1:39 Comment(0)
W
20

iron router >= 1.0

A route's query parameters are available as properties of this.params.query.

If your URL looked like:

/posts/5?sort_by=created_at

then this.params.query.sort_by would equal 'created_at'.


iron router < 1.0

A route's query parameters are available as properties of this.params.

If your URL looked like:

/posts/5?sort_by=created_at

then this.params.sort_by would equal 'created_at'.

Wristband answered 14/4, 2014 at 2:34 Comment(8)
I thought that too but the query parameters are not showing up in the this.paramsSqually
It should work, try using this debug hook Router.onBeforeAction(function(){console.log(this.params);}); and typing any URL with query parameters to access your app, you'll see them in the console.Countershading
@Countershading - as I said, this.params is emptySqually
@pat It would be really helpful if you could update your question with a simplified version of the URL and the route.Wristband
it turns out that this.params.sort_by is set but the javascript console does not show it when doing this.params by itselfSqually
In the console it shows params as an Array of 0 length yet you can expand it and see that it has the queries set as properties. I'm not sure if you can iterate over the params without knowing what they are.Threegaited
This is now this.params.query.sort_by in Iron Router v1.0.Ripe
Since this answer is no longer right, can we shift the correct answer?Peraea
L
45

Just call

Router.current().params //params is the dict you wanted

in Iron Router 7.1+

Laaland answered 20/6, 2014 at 3:5 Comment(1)
You are a life saver. Thanks a ton!Ramekin
P
24

Interestingly three answers and no one offered the complete answer.

Iron-Router 1.0.x

From within a route, use:

// URL: http://example.com/page/?myquerykey=true
this.params.query   // returns the full query object
this.params.query.myquerykey   // returns a particular query value

Similarly, outside of the route (but still inside the client code), and inside your template, use:

// URL: http://example.com/page/?myquerykey=true
Router.current().params.query
Router.current().params.query.myquerykey

Query parameters, not to be confused with parameters passed via the URL.

Peraea answered 30/4, 2015 at 6:26 Comment(2)
well done, this should be accepted as the correct, and complete, answer.Loreenlorelei
Good Answer in case you want to get the values in Template Helpers, specially when you want to get the query values outside the routeInjection
W
20

iron router >= 1.0

A route's query parameters are available as properties of this.params.query.

If your URL looked like:

/posts/5?sort_by=created_at

then this.params.query.sort_by would equal 'created_at'.


iron router < 1.0

A route's query parameters are available as properties of this.params.

If your URL looked like:

/posts/5?sort_by=created_at

then this.params.sort_by would equal 'created_at'.

Wristband answered 14/4, 2014 at 2:34 Comment(8)
I thought that too but the query parameters are not showing up in the this.paramsSqually
It should work, try using this debug hook Router.onBeforeAction(function(){console.log(this.params);}); and typing any URL with query parameters to access your app, you'll see them in the console.Countershading
@Countershading - as I said, this.params is emptySqually
@pat It would be really helpful if you could update your question with a simplified version of the URL and the route.Wristband
it turns out that this.params.sort_by is set but the javascript console does not show it when doing this.params by itselfSqually
In the console it shows params as an Array of 0 length yet you can expand it and see that it has the queries set as properties. I'm not sure if you can iterate over the params without knowing what they are.Threegaited
This is now this.params.query.sort_by in Iron Router v1.0.Ripe
Since this answer is no longer right, can we shift the correct answer?Peraea
O
17

In Iron Router 1.0.0, you need to use

this.params.query.YOUR_PARAMETER_NAME

to get it

For example, if you route is /xxx/?a=b

this.params.query.a

outputs 'b'

Overdraw answered 22/10, 2014 at 8:38 Comment(0)
F
2

try tihs:

 Router.current().params.parametername;

and in router.js file routing must be:

route(routername/:parametername)
Flogging answered 11/5, 2016 at 8:4 Comment(0)
G
1

Ensure that if you are using Router.go that your first parameter is a template name, and not a path. query parameters are not passed if you specify a path.

Goer answered 25/7, 2015 at 2:25 Comment(0)
G
1

Encoded URI undefined Solution:

The better way to get the query parameters object is:

this.request.query.MyParam

Using the suggested option of:

this.params.query.MyParam

Is ok as long as you are not working with encodedURI parameters, when using this option with encodedURI parameter, the parameter will be equal to undefined.

Example below:

{ // console.log(this.params.query)
    product: 'Chair',
    ip: '172.0.1.183',
    message: 'My Little Chair',
    request: '100% Discount',
    severity: '4',
    api_key: 'XXXXX' 
}

{ // console.log(this.params.query)
    product: 'Chair',
    ip: '172.0.1.183',
    message: 'My Little Chair',
    request: 'undefined', // NOTICE THIS CHANGED TO UNDEFINED!
    severity: '4',
    api_key: 'XXXXX' 
}

Original Query String:
?product=Chair&ip=172.0.1.183&message=My Little Chair&request=100%25%20Discount&severity=4&api_key=XXXXX
Ghat answered 19/5, 2016 at 11:49 Comment(0)
T
0

You can pass queries like this depending on where you accessing the router:

In the template

{{pathFor 'routeName' query='queryName=queryValue'}}

In the helper

Router.go ('routeName',{},{query: 'queryName=queryValue'}

Note: the empty object between the routeName and the query is if you want to specify any parameters (refer to the full docs to see the difference).

If you would like to pass multiple queries do it like this:

query: 'queryName1=queryValue&queryName2=queryValue'

Don't use spaces and remember to use the & sign.

Transcend answered 31/7, 2015 at 19:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.