Kami
Kami

Reputation: 1

Meteor: Querying a collection based on a URL parameter

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.

meteor question - screenshot

I have tried querying the MongoDB 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>

I have tried all sorts of ways but the Collection fields never appear in the HTML file.

Thanks for your help, a newbie here.

K.

Upvotes: 0

Views: 109

Answers (1)

Michel Floyd
Michel Floyd

Reputation: 20246

Most likely you are not publishing all Babysitters to the client so your .findOne() returns nothing.

This is a common router pattern where you want to display a single document which is not normally published. A good way to solve this in i-r is to waitOn a subscription to the individual document:

waitOn: function(){
    return Meteor.subscribe('oneBabysitter', this.params._id);
}

On the server, publish:

Meteor.publish('oneBabysitter',function(_id){
  return Babysitters.find(_id);
});

Note that even though this publication is only going to return a single document you still have to do a .find() and not a .findOne() because publications need to return a cursor or array of cursors, not objects.

Upvotes: 0

Related Questions