meteor-aggregate: Does not work with Meteor.users (my code is on server side)

I have seen issue #4, but I’m afraid that this package still does not work with Meteor.users. I’ve spent the whole day trying to get this to work, but it simply won’t. (I have the very latest Meteor 1.2 version, for what it’s worth).

Background: I had thought it would be a good idea to add customer orders to the user.profile - seemed like an obvious place to put them. So there is an orders[] array under user.profile. So to see the latest orders I need to unwind this array from all the user records.

The following code works nicely in the mongo console: > db.users.aggregate([{$match: {}}, {$project: {profile: 1}}, {$unwind: ‘$profile.orders’}]);

So now here’s my (very simple) server code:

Meteor.methods({
    getOrders: function(){
        return Meteor.users.aggregate([{$match: {}}, {$project: {profile: 1}}, {$unwind: '$profile.orders'}]);
    }
});

This fails with the following error message:

I20151021-21:54:50.969(2)? Exception while invoking method 'getOrders' TypeError: Object [object Object] has no method 'aggregate'
I20151021-21:54:50.969(2)?     at [object Object].Meteor.methods.getOrders (meteor://💻app/server/methods.js:5:1)
I20151021-21:54:50.970(2)?     at maybeAuditArgumentChecks (meteor://💻app/livedata_server.js:1692:12)
I20151021-21:54:50.970(2)?     at meteor://💻app/livedata_server.js:708:19
I20151021-21:54:50.970(2)?     at [object Object]._.extend.withValue (meteor://💻app/packages/meteor/dynamics_nodejs.js:56:1)
I20151021-21:54:50.970(2)?     at meteor://💻app/livedata_server.js:706:40
I20151021-21:54:50.970(2)?     at [object Object]._.extend.withValue (meteor://💻app/packages/meteor/dynamics_nodejs.js:56:1)
I20151021-21:54:50.971(2)?     at meteor://💻app/livedata_server.js:704:46
I20151021-21:54:50.971(2)?     at tryCallTwo (/Users/malcolm/.meteor/packages/promise/.0.5.0.15j1xzs++os+web.browser+web.cordova/npm/node_modules/meteor-promise/node_modules/promise/lib/core.js:45:5)
I20151021-21:54:50.971(2)?     at doResolve (/Users/malcolm/.meteor/packages/promise/.0.5.0.15j1xzs++os+web.browser+web.cordova/npm/node_modules/meteor-promise/node_modules/promise/lib/core.js:171:13)
I20151021-21:54:50.971(2)?     at new Promise (/Users/malcolm/.meteor/packages/promise/.0.5.0.15j1xzs++os+web.browser+web.cordova/npm/node_modules/meteor-promise/node_modules/promise/lib/core.js:65:3)

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 17

Most upvoted comments

I can do this manually to get it working - with the following code:

Meteor.methods({

getOrders: function(){

    var rawUsers = Meteor.users.rawCollection();
    var aggregateQuery = Meteor.wrapAsync(rawUsers.aggregate, rawUsers);
    var pipeline = [
        {$match: {}},
        {$project: {profile: 1, emails: 1}},
        {$unwind: '$profile.orders'}
    ];
    var result = aggregateQuery(pipeline);

    return result;
}


});

This works. Even though it appears to be the much the same as the code in the package.