Querying a collection by reading a parameter off the URL

Hello,

I am trying display the unique profile of a babysitter (i.e: babysitter username, city, postal code, etc … ) from a schema/collection called “Babysitters” .

The URL correctly contains the babysitter unique _id, say:

http://localhost:3000/test/PqMviBpYAmTA2b5ec

but I don’t manage to retrieve all the other fields.

The MongoDB is queried in two files: routes.js and the template test.js

1) in routes.js

Router.route(‘/test/:_id’, {
name: ‘test’,
data: function () {

  return Babysitters.findOne({ _id: this.params._id });

}
});

2) in test.js

Template.test.helpers({

data: function () {

	//sitterusername = this.params.sitterusername;
	 //console.log(this.params._id );
 
  return Babysitters.findOne( { _id: this.params._id });
}

});

the html file: test.html:

< template name=“test” >
{{#with data}}
< ul >
< li >< img src=“/” {{photourl}} height=“100” width=“100” >< /li >
< li >Babysitter username: {{ sitterusername }}< /li >
< li >Presentation: {{ presentation }}< /li >
< li >City: {{ city }}< /li >
< li >Postal Code: {{ postalcode }}< /li >
< li >Mother tongue: {{ mothertongue }}< /li >
< li >Languages spoken {{ languagesspoken }}< /li >
< li >Experience {{ experience }}< /li >
< li >Homework help: {{ homeworkhelpavailable }}< /li >
< li >Hourly wages: {{ hourlywages }} €/h< /li >
< /ul >
{{ /with }}
< /template >

Thanks for your time and suggestions, I am just a meteor newbie.

Kâmi

Iron Router’s data function is used to set the data context of your template. In your example since you’re already returning

return Babysitters.findOne({ _id: this.params._id });

you don’t need to create a helper on your template to do this as well. So you could change things as:

1) in routes.js:

Router.route('/test/:_id', {
  name: 'test',
  data: function () {
    return Babysitters.findOne({ _id: this.params._id });
  }
});

2) Remove the data function from your helper.

3) test.html:

<template name="test"> 
  <ul>
    <li><img src="/" {{photourl}} height="100" width="100"></li>
    <li>Babysitter username: {{sitterusername}}</li>
    ...
  </ul>
</template>

All of the above being said, if you’re new to Meteor you should check out the URLs and Routing section of the Guide (and consider switching from Iron Router to Flow Router).

Thanks hwillson.

I have gone a bit further thanks to generous Stackoverflow contributors but I am not quite there yet.

the thread: http://stackoverflow.com/questions/36353752/meteor-querying-a-collection-based-on-a-url-parameter

Any other suggestions ?

Why switch to Flow Router ?

Thanks, K.