How to use URL parameters with Meteorjs

How to use URL parameters with meteor.

The URL may look like this: http://my-meteor.example.com{000 ? task_name=abcd1234

I want to use "task_name" (abcd1234) in a mongodb request in a meteor application.

eg.

Template.task_app.tasks = function () { return Tasks.find({task_name: task_name}); }; 

Thanks.

+7
meteor url-parameters
source share
1 answer

You will probably want to use a router to take care of the paths and display specific patterns for different paths. An iron router is best for this. If you are not using it already, I would highly recommend it.

Once you use an iron router, getting query strings and URL parameters is very simple. You can see the documentation section here: https://github.com/iron-meteor/iron-router/blob/devel/Guide.md#route-parameters

In the example you gave the route, it will look something like this:

 Router.map(function () { this.route('home', { path: '/', template: 'task_app' data: function () { // the data function is an example where this.params is available // we can access params using this.params // see the below paths that would match this route var params = this.params; // we can access query string params using this.params.query var queryStringParams = this.params.query; // query params are added to the 'query' object on this.params. // given a browser path of: '/?task_name=abcd1234 // this.params.query.task_name => 'abcd1234' return Tasks.findOne({task_name: this.params.query.task_name}); } }); }); 

This will create a route that will display the "task_app" template with the data context of the first task that matches the name of the task.

You can also access url parameters and other route information from template assistants or other functions using Router.current () to get the current route. So, for example, in the helper you can use Router.current().params.query.task_name to get the current name of the task. Router.current () is a reactive element, so if it is used in reactive computation, the computation will be restarted when any changes are made to the route.

+18
source share

All Articles