Low Level Publications - directly controlling a published record se

Help please!!!
Background: Meteor React application …
Requirement: Ultimately I need to add aggregated results to a basic publication.
Can achieve this via Meteor method, but really need the dataset to be reactive.
So I’ve been trying to use the low level API

I’ve tried every combination I can find thru all the forums, but fail to see the results on the client.
Here’s a cut down simplified version …

Client Side …

export default withTracker(() => {
  const subscription = Meteor.subscribe('combinations');
  return {
    loading: !subscription.ready(),
    combinations: CombinationCollection.find({}).fetch(),
  };
})(Combinations);

Server side …

Meteor.publish('combinations', function getCombinations() {
  const cursor = Combinations.find({ owner:this.userId });
  const self = this;
  cursor.forEach(function addStats(doc) {
    const row = doc;
    const id = row._id;
    delete row._id;
    row.statistics = 'some stats';
    console.log('row', id, row);
    self.added('combinations', id, row);
  });
  return self.ready();

the ‘console.log(‘row’, id, row)’ is reporting the correct changes, but as mentioned nothing is returned to the Client.

Ps.
I’ve tried/failed all combinations of cursor.observe; cursor.observeChanges
Obviously missing something obvious :slight_smile:

Cheers in advance
Rob

Here’s an example that worked for me:

It’s using two cursors because I also need a regular publication at the same time for my use case. The relevant part is the lowlevel observer.

Thank you - I’ll investigate tomorrow.
Did you end up using tunguska-reactive-aggregate?

Perhaps I need to ask a different question …
I currently have a Collection called Combinations
image

Pseudocode …
Loop through Combinations and for each templateName
Perform a complex calculation => result

Display templateName, result

I can achieve this via Meteor Methods
Is it possible to do this via a Publication?

I tried this

Meteor.publish('combinations', function getCombinations() {
  const addStatistics = (fields) => {
    const newFields = fields;
    newFields.statistics = 'some stats';
    console.log('fields', newFields);
    return newFields;
  };
  const statisticsCursor = Combinations.find({ combinationName: 'Adelaide Zones' });

  statisticsCursor.observeChanges({
    added: (id, fields) => this.added('combinations', id, addStatistics(fields)),
  });
  this.ready();
  console.log('object', statisticsCursor.fetch());
  return statisticsCursor;
});

image

But this is what gets delivered to the client

image

Do not return the cursor

From the docs:

If a publish function does not return a cursor or array of cursors, it is assumed to be using the low-level added/changed/removed interface, and it must also call ready once the initial record set is complete.

Hi rjdavid,
Agreed, but I believe I am returning a cursor?

Try removing the line returning the cursor and check your results

Remove this line

return statisticsCursor;

Don’t forget to stop the observer. Otherwise you will have a memory leak

const observer = statisticsCursor.observeChanges({
    added: (id, fields) => this.added('combinations', id, addStatistics(fields)),
});
this.onStop(() => { observer.stop(); });