代码之家  ›  专栏  ›  技术社区  ›  Stan Lindsey

如何基于第一个集合中的数据发布第二个集合

  •  2
  • Stan Lindsey  · 技术社区  · 10 年前

    我正在努力根据第一个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 回复  |  直到 10 年前
        1
  •  1
  •   Stan Lindsey    10 年前

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

    var authorId = post.fetch()[0].authorId;
    
        2
  •  0
  •   Hook    10 年前

    发布订阅时,应返回游标。请尝试以下操作:

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