Problem with publication for a single post

I apologize in advance for my English :).

I am novice to meteor js. To discover I created a small application. I created a collection ‘posts’ with a title, contents and a reference section (rubrique) and a collection ‘rubrique’. I manage to publish the whole collection or all posts by section but i can’t publish for a single post. I put you down the script. If somebody can help me :slight_smile: thank you in advance.

//server publication.js ##############################################################
//server section
Meteor.publish(‘rubriques’, function () {
return Rubriques.find();
});

//publish all or by section
Meteor.publish(‘posts’, function (option) {
if (option == “all”) {
return Posts.find();
} else {
return Posts.find({ref: option});
}

//server for single post
Meteor.publish(‘single-post’, function (postId) {
check(postId, { _id: String }); return Posts.find({}, options);
});

//client router.js ##########################################################
Router.route(’/rubriques/:ref’, {
name: ‘postPage’,
data: function () {
return Rubriques.findOne({ref: this.params.ref});
},
waitOn: function () {
return Meteor.subscribe(‘posts’, this.params.ref);
}
});
Router.route(’/allPosts’, {name: ‘postAll’,
data: function () {
return Meteor.subscribe(‘posts’, ‘all’);
}
});

Router.route(’/voir/:_id’, {
name: ‘postVoir’,
data: function () {
return Meteor.subscribe(‘single-post’, ‘’+this.params._id+’’);
}
});

Meteor.publish(‘single-post’, function (postId) {
  check(postId, String);
  return Posts.find(postId, {limit:1});
});

thank you for your reply, I added the limit but nothing changes. i tried to play the request on console of my web browser after click on ‘single-post’ for testing.

Posts.findOne({_id:‘nj3MA72osBWmnnbcR’});
undefined

even though you pass in the postId, you still return the entire collection? Shouldn’t it be

//server for single post
Meteor.publish(‘single-post’, function (postId) {
  check(postId, { _id: String }); return Posts.find(postId);
});
1 Like

@jamgold has it right, you always want to publish a cursor, and not a result.

Unless you have a complicated edge case, you always want to use Find in your publication, and then use FindOne on the client where you want the data.

Thank you very much in all for your answer, I simplified the publication and i modified the subscrition too. I think that the code is not great clean but it works well. I leave a code for informations

// On server

//Ensemble des Posts, on passe un parametre ‘all’ pour tous les posts ou ‘ref’ pour avoir posts par rubriques
Meteor.publish(‘posts’, function (option) {
if(option == “all”){
return Posts.find();
}else{
return Posts.find({ref: option});
}
});

//On remonte la collection rubriques
Meteor.publish(‘rubriques’, function () {
return Rubriques.find();
});

//On client

Router.route(’/rubriques/:ref’, {
name: ‘postPage’,
data: function () {
return Rubriques.findOne({ref: this.params.ref});
},
waitOn: function () {
return Meteor.subscribe(‘posts’, this.params.ref);
}
});

Router.route(’/allPosts’, {name: ‘postAll’,
data: function () {
return Meteor.subscribe(‘posts’, ‘all’);
}
});

Router.route(’/voir/:_id’, {
name: ‘postVoir’,
data: function () {
Meteor.subscribe(‘posts’, ‘all’);
return Posts.findOne({_id: ‘’ + this.params._id + ‘’});
}
});

1 Like