发布/订阅Mongo汇总查询时遇到问题 [英] Trouble publishing/subscribing to a Mongo aggregate query

查看:95
本文介绍了发布/订阅Mongo汇总查询时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Meteor和Iron-Router发布以下mongo查询(在服务器文件夹中):

Using Meteor and Iron-Router, I'm trying to publish the following mongo query (in the server folder):

Meteor.publish("getTestList", function() {
  return Tests.aggregate(
    [{
      $project : {
        "name" : 1,
        "description" : 1,
        "testNumber" : 1
      }
    }, {
      $sort : {
        "testNumber" : 1
      }
    }
  ])
});

请注意,我已经在meteor mongo控制台工具中测试了此查询,并且该查询在此正常工作. Tests也是

Note that I have tested this query in the meteor mongo console tool and it works fine there. Also Tests is

Tests = new Mongo.Collection("tests")

我正在这样订购路由器:

and I am subscribing in the router like this:

Router.route('/user', {

  waitOn: function() {
    // return [Meteor.subscribe("tests")];
    return [Meteor.subscribe("tests"),Meteor.subscribe("getTestList")];
  },

  action: function() {
    if (!this.ready()) {
      this.render('loading');
    }
    else {
      Session.set("testName", "blablabla")
      Session.set("submitted", false)
      this.layout('BasicLayout')
      this.render('UserPortal')
    }
  }
});

如果我导航到/user,则它永远不会通过加载...屏幕.控制台中没有错误,并且如果我仅订阅tests而不订阅getTestList(即代码中的注释行),则UserPortal模板确实会加载,但是出现控制台错误,指出了Tests.aggregate不存在.

And if I navigate to /user then it never gets passed the loading...screen. There are no errors in the console, and if I subscribe only to tests and not to getTestList (i.e. the commented out line in the code), then the UserPortal template does load but I get a console error stating the Tests.aggregate does not exist.

我做错了什么?

推荐答案

Meteor还不支持聚合.不过,您可以使它以这种方式工作:

Meteor does not support aggregation yet. You can get it to work this way though:

添加聚合包:meteor add meteorhacks:aggregate

请改用Meteor.call/Meteor.methods,因为此时聚合结果是静态的.不支持任何反应.

Use Meteor.call/Meteor.methods instead, since a aggregation result is static at this point. No reactivity supported.

服务器端

Meteor.methods({
    "getTestList" : function() {
        return Tests.aggregate(
        [{
            $project : {
            "name" : 1,
            "description" : 1,
            "testNumber" : 1
          }
        }, {
          $sort : {
            "testNumber" : 1
          }
        }
      ])
    }
});

客户端:

您的模板

Template.xx.onCreated(function() {

    Meteor.call("getTestList", function(err, result) {
        Session.set("testlist", result);
    });
});

然后,您可以通过签出Session.get("testlist");

Then you can access the data 'reactively' (when its ready) by checking out Session.get("testlist");

这篇关于发布/订阅Mongo汇总查询时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆