如何基于第一个

时间:2016-01-24 16:22:55

标签: mongodb meteor

我正在努力根据第一个mongo查询中的数据字段向特定作者返回发布。

在下面的帖子订阅工作绝对正常,但作者订阅永远不会返回任何内容。我假设它与异步代码有关。

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

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

});

2 个答案:

答案 0 :(得分:1)

找到答案: 正如Brian所说,我将post变量视为对象而不是游标。出版物要求返回游标,因此获取authorId的最佳方法是使用

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

答案 1 :(得分:0)

发布订阅时,您应该返回一个游标。试试这个:

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

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