How to use the subscribe that it has only one item?


#1
Meteor.publish('postDetails', function(postId) {
    return Posts.find(postId);
    // In fact, I want to findOne
})



var postDetails = $meteor.collection(Posts).subscribe('postDetails', postId);

How to get the data I want from postDetails ?


#2

where you got that ugly long subscribe sentence?
postDetails is just handle pointing to subscription.
after subscription is ready, you can access the post for example
Posts.findOne();
It should work in browser console.

Still why not Meteor.subscribe('postDetails', postId); ?


#3

Could you tell more details ?


#4

Subscribe itself does not return document. It returns only handle on which you can call .stop() to end subscription.
To get data, you need to query client side collection and for this Posts.findOne() should work for you.


#5

I got undefined from Posts.findOne() ?


#6

I am using Angular-Meteor


#7

you need to define which One you want to find.

Posts.findOne({_id: "ysywiwwhs"})

or

Posts.findOne({age:{$gt: 50}})

now your collection can contain many documents with age>50, but this will only return the first of the documents it finds.


#8

if it is only a one item you’d like to retrieve why not use this msavin:fetcher package. you still need to make a proper publish function though which I think where you are having problem