Passing request parameters using Router.go in an iron router

I am trying to pass request parameters to Router.go as below:

var filter = 'abc'; var path = Router.current() && Router.current().path; Router.go(path, {query: {filter: filter}}); 

But this does not change the url, it still loads the current path without a query string. But if I add the query parameter manually to the following path:

 Router.go(path+'?filter='+filter); 

this works great. But since I'm trying to load the same page with some filtered data. Thus, the click filter button re-adds the filter string again and again to the path.

What is the correct way to pass a query string using an iron router?

+8
meteor iron-router
source share
4 answers

Right there in the docs

 Router.go('post.show', {_id: 1}, {query: 'q=s', hash: 'hashFrag'}); 

The above JavaScript will navigate to this URL:

 /post/1?q=s#hashFrag 

https://github.com/iron-meteor/iron-router/blob/devel/Guide.md#named-routes

+9
source share

Parameters Router.go(path, params, options) . Part of the request should go to the options parameter, so try the following: Router.go(path, {}, {query: {filter: 'filter='+filter}}) .

EDIT

Answer updated according to Robins comment below.

+4
source share

I found that if your first parameter in Router.go is the path, the request filter is not passed in place of the template name. Use the template name:

 Router.go(templatename, {_id: 1}, {query: 'q=s', hash: 'hashFrag'}); 
+2
source share

Try the following:

 var path = Router.current() && Router.current().route.originalPath; 

This should give you a path without a query string.

0
source share

All Articles