Client no data ,use aggregate


#1

use meteorhacks:aggregate


server

Meteor.publish("OrdersGroupJoinDraws",function(){
let self=this;
let pipline=[.....];
Orders.aggregate(pipline).forEach((item)=>{
self.added("OrdersJoinDraws",item._id+"",item);
});
self.ready();
});

client


Meteor.subscribe("OrdersGroupJoinDraws",function(){

Orders.find().fetch();  // result is [];
OrdersJoinDraws.find().fetch();  // result is [];

});

how to get data;


#2

Have you tried putting some console.logs within the server’s forEach block to see if any records are added as a result of the aggregation?

I’ve not got any experience of using aggregate within publications, I’ve only used it within methods.

One thing to note is since Meteor 1.7.0.1 the aggregate function returns an AggregationCursor instead of an array, so now it’s necessary to use toArray() on the result. Read more here: https://docs.meteor.com/changelog.html#v170120180529

I found that the example code in the changelog above didn’t work for me because toArray() also returns a promise. Instead I did this:

function getAggregate(pipeline) {
	const users = Meteor.users.aggregate(pipeline);
	return Meteor.wrapAsync(users.toArray, users)();
}


#3

work!!!

 const ll = Orders.aggregate(pipline);
 console.log(Meteor.wrapAsync(ll.toArray, ll)());

but how to publish this array?:sweat_smile:
thanks your explain


#4

I’m glad toArray and wrapAsync worked for you!

Sorry, I’m not familiar with publishing the result of aggregations - I’m not even sure if the result is reactive. In my case I didn’t need reactivity on the result so I used a method to send the array to the client.

Maybe see here: Publishing aggregations is REAL!

Good luck!


#5

:grinning::grinning::grinning:
thanks


#6

If you do not want reactive aggregations (e.g. you’re using Meteor methods), you should really use the underlying MongoDB methods, rather than rely on an unmaintained package which does not take account of changes in the MongoDB library, and just wraps those methods in a small amount of Meteor sugar.

Alternatively, if you do want reactive aggregations (pub/sub), you will find an atmosphere package to be a simpler way to achieve what you want. Look at tunguska:reactive-aggregate for a modern (ES7) way to do this in Meteor.


#7

tanks :slight_smile:
:laughing::laughing::laughing::laughing: