JSFiddle - React, Tailwind, and code Playground

by Billybobbonnet

JavaScript

var self = this;
var itemCount = 0;
myCollection.find() //unfiltered find because we need to observe all the fields
            .observeChanges({
                changed: function (id, field) {
                    var raw_document = myCollection.findOne({_id: id});
                    switch (field) {
                        case "item":
                            itemCount = raw_document.item.length;
                            break;
                        default:
                        }
                    }
                })
             .forEach(function (entry) { //I iterate on each document in order to add its item_count field
                self.added("item_count", entry._id, raw_document.item.length);
             })
            .find({}, { item: 0 }) // I filter my item array

// I update the values in case a change has been observed
self.added("item_count", entry._id, itemCount);
self.ready();`