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
Cheers in advance
Rob
Here’s an example that worked for me:
Thanks for the feedback. Yes, I solved this using observeChanges and an added hook in the meantime. However, with my current solution, I actually need two cursors in my ownUser publication: one for the user object without the service configuration (in order to not publish sensitive data to the client) and one that just observes changes on the services key, clears all data inside these keys and sends just an empty object for every service key to the client:
// publish social services, but with…
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
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;
});
But this is what gets delivered to the client
rjdavid
November 6, 2020, 12:57am
5
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;
ksinas
November 10, 2020, 1:33pm
8
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(); });