Stan Lindsey
Stan Lindsey

Reputation: 485

How to publish a second collection based on data in first

I am struggling to return a publication to a specific author based on data field in the first mongo query.

In the below the Posts subscription works absolutely fine, but the Authors subscription never returns anything. I am assuming it has something to do with async code.

Meteor.publish("postAndAuthor", function (postId) {
  check(postId, String)
  var post = Posts.find({_id: postId});
  var authorId = post.authorId;

  return [
    book,
    Authors.find({_id: authorId})
  ];

});

Upvotes: 2

Views: 30

Answers (2)

Hook
Hook

Reputation: 37

When publishing a subscription you should be returning a cursor. Try this instead:

Meteor.publish("postAndAuthor", function (postId) {
  check(postId, String)
  var post = Posts.find({_id: postId});
  var authorId = post.authorId;

  return Authors.find({_id: authorId}):
});

Upvotes: 0

Stan Lindsey
Stan Lindsey

Reputation: 485

Found the answer: As the comment Brian stated, I am treating the post variable an object not a cursor. Publications require cursors are returned though so the best way to get the authorId is using

var authorId = post.fetch()[0].authorId;

Upvotes: 1

Related Questions