Meteor: Adding An Associated Record Doesn't Work
I have Posts and Comments and I'm trying to build the UI for adding a comment to a post using React and Meteor. When I submit a comment, the new comment appears and then immediatel
Solution 1:
That is because in your publication post
is evaluated only once and therefore your cursor is not "reactive" in the sense that it will not re-evaluate the query when you comments
change, as it is fetched.
There are several ways to do this, either directly, using a package or redesign to avoid it.
- Do it directly, as demonstrated in this video. I would not recommend this unless you really know what you are doing and have a very good reason to do so. It involves directly observing your first query and manually pushing updates to the client when something changes. I think that David Weldon summarizes it quite well in this answer.
- De-normalize the data, which will require you to keep track of the duplicates as the canonical data source changes. This moves the complexity to another area of the code, and is not very easy to implement. There are certain design patterns, such as CQRS, that allow you to achieve this in a fairly robust and performant way, but they are not easy to grasp and implement.
- Use a package, such as reywood:publish-composite, that makes it a little less painful that the manual option at the price of potential performance penalty.
- Change your data structure to allow easy publication. That depends on your use-case and the rest of the requirements. In your case, having an
articleId
in each comment document will make publishing all of the comments for each article trivial. You may want to add an index to that field to boost performance.
BTW, the "reactive join" problem is still unsolved and will likely remain so since a new GrapnQL-based data model is coming to Meteor via Apollo soon.
Post a Comment for "Meteor: Adding An Associated Record Doesn't Work"