Mongo general query post / subscribe issues

Using Meteor and Iron-Router, I am trying to post the following mongo request (in the server folder):

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

      

Please note that I have tested this request in a console tool meteor mongo

and it works fine. There Tests

is also

Tests = new Mongo.Collection("tests")

      

and I subscribe to 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')
    }
  }
});

      

And if I go to / user then it never goes through the loading screen ... There are no errors in the console and if I only subscribe to Tests

and not to getTestList

(i.e. the commented line in the code) then the template is UserPortal

loaded. but I am getting a console error stating Tests.aggregate

does not exist.

What I did wrong?

0


source to share


1 answer


Meteor does not support aggregation yet. You can make it work like this:

Add to aggregation package: meteor add meteorhacks:aggregate

Use Meteor.call

/ Meteor.methods

instead, as the result of the aggregation is static at this point. Reactivity is not supported.

server side

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

      



Client side:

Your template

Template.xx.onCreated(function() {

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

      

You can then access the data "reactively" (when ready) by checking Session.get("testlist");

+3


source







All Articles