1

I have a complicated data structure being built by queries on multiple collections and published.

It is working great for the initial creation, and on my local machine all the changes observed are reflected in the client as expected. However, in my staging environment I get the following error from mini-mongo when a change is observed

Uncaught Error: When replacing document, field name may not contain '.'(…)

The publishing code looks like this, where pub is the this from a Meteor.publish and rootObj is a reference to an Object in memory which gets properties modified but never has it's reference destoryed.

function _republish(pub, rootId, rootObj, handles, startup) {
    // cleanup handles
    if (handles.foo) {
        handles.foo.stop();
    }
    // some query which could depend on rootObj/other calculated values
    let cursor = SubColl.find({_id: {$in: bar}});
    handles.foo = cursor.observeChanges({
        removed(_id) {
            rootObj.bar = rootObj.bar.filter(o => o._id !== _id);
            pub.changed('foobarbaz', rootId, {bar: rootObj.bar})
        },
        changed(_id, fields) {
            const index = rootObj.bar.findIndex(line => line._id === _id);
            const changed = {};
            _.each(fields, (value, field) => {
                rootObj.bar[index][field] = value;
                changed[`bar.${index}.${field}`] = value;
            });
            pub.changed('foobarbaz', rootId, changed);
        },
        added(_id, fields) {
           rootObj.bar.push(_.extend({}, fields, {_id}));
           if (!startup) {
               // deeper children stuff
               pub.changed('foobarbaz', rootId, {bar: rootObj.bar});
           }
        }
    });

    // deeper children stuff

    startup = false;
    // if startup was true, expect caller to publish this
}

As we can see, the publish works fine when I'm pub.changeding on just bar, but attempting to update a specific subdocument field (e.g. bar.0.prop) results in the inconsistent behaviour

If possible I want to avoid re-publishing the whole of bar as it is huge compared to updating a simple property.

How can I publish the change to a single field of a subdocument?

Paul S.
  • 64,864
  • 9
  • 122
  • 138
  • I don't think you can. You may patch DDP to do what you want, but not out of the box. My idea of a DDP patch would be adding a message that will deliver the changes and when processed on the client get the current data on the client, create the patch and will issue a normal "updated" DDP message that will be processed as usual. It won't be pretty, but it may work for very specific use cases. – MasterAM Dec 15 '16 at 11:47
  • Thanks @MasterAM, I don't think patching DDP would be a good idea in this situation; the legacy behaviour was to re-transmit the whole thing so I may continue to use that – Paul S. Dec 18 '16 at 23:41

0 Answers0