Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FindOne instead find in post.single publication #99

Closed
johngonzalez opened this issue Apr 29, 2016 · 1 comment
Closed

FindOne instead find in post.single publication #99

johngonzalez opened this issue Apr 29, 2016 · 1 comment

Comments

@johngonzalez
Copy link

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
Copy link
Author

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant