I don’t think there is anything specific in the code being that it’s extremely simple, and I have found the source of the problem to be on reactive update of collection.find() which updates twice, first time data is correct, but on last update it isn’t.
Basic Specs
What I am doing is simple and straight forward:
- I have groups and documents.
- The documents are nested within groups; so documents have a
groupId
field that stores the group it belongs to, and anorder
field which stores the order. - Whenever a document is moved to a new group, the
order
for all documents in both old/new groups is updated, and thegroupId
for the moved document is updated.
Simplest Reproducible Example
Here is an extremely simple example of where this fails, and the reactive data returns the incorrect values, even though it’s correct in the db!!
- I have two groups, and two documents
- Each group has one document as a child
- I move one document out of a group into the other, leaving one group empty, and the other with two documents… and here is what happens
Output from console.log()
collection.update()
is called two times; one to update the order
& new groupId
of the moved item, and another to update the order
of the document that already existed in the group
>>> collection.update()
{_id: "dAQQu7ChSQHSbFyd8", order: 0, groupId: "nYF2pdex62ChyAtNq"}
{_id: "fZR7TjaoRGbN8Mz5B", order: 1}
Template helper that renders the groups/documents receives reactive update and is called twice.
Here is the output from collection.find().fetch()
for the first update – everything is correct up until this point.
>>> helper() called
{_id: "dAQQu7ChSQHSbFyd8", order: 0, groupId: "nYF2pdex62ChyAtNq"}
{_id: "fZR7TjaoRGbN8Mz5B", order: 1, groupId: "nYF2pdex62ChyAtNq"}
Helper receives reactive update again (after the one above), and now the groupId
value of the document moved is wrong?!
>>> helper() called
{_id: "dAQQu7ChSQHSbFyd8", order: 0, groupId: "SmK8bJpgB8MQxGKWy"}
{_id: "fZR7TjaoRGbN8Mz5B", order: 1, groupId: "nYF2pdex62ChyAtNq"}
At this point there is twitch in the UI and the item is moved back to it’s original group – but the db has the correct data, and if the page is reloaded it displays properly.
Any guidance, or help would be greatly appreciated!