mantrajs / mantra-sample-blog-app

A sample blog app built with Mantra
http://mantra-sample-blog-app.herokuapp.com/
MIT License
296 stars 104 forks source link

FindOne instead find in post.single publication #99

Closed johngonzalez closed 8 years ago

johngonzalez commented 8 years ago

I think is better use findOne instead find to fetch the post https://github.com/mantrajs/mantra-sample-blog-app/blob/master/server/publications/posts.js

  Meteor.publish('posts.single', function (postId) {
    check(postId, String);
    const selector = {_id: postId};
    return Posts.find(selector);
  });
johngonzalez commented 8 years ago

You are fine!. With findOne instead find I get the follow error: seleccion_364 But. Why is not possible I use findOne?, I want to find a single document, should be possible use it appropriately named findOne. I self answer the question: Publications need return a cursor (find), not a object (findOne). According with meteor documentation (http://docs.meteor.com/#/full/findone) findOne is Equivalent to find(selector, options).fetch()[0] with options.limit = 1.

So I change the code in publications/posts.js to:

  Meteor.publish('posts.single', function (postId) {
    check(postId, String);
    const selector = {_id: postId};
    // here the change
    const options = {limit: 1};
    return Posts.find(selector, options);
    //
  });

It works! and I think that it is more efficient :)