3

I can't find anything in the docs, but in case I'm missing something, is there any way in Meteor to enrich documents inserted from the client on the server before they're sent to Mongo.

The use-case for this is to handle things like setting up timestamps and other (for server-side use only) fields, without having to set them as part of the document on the client.

The obvious method would be to use Meteor.call('addMyNewRecord', publicFields) then insert solely from the server-side, but I quite like having the minimongo api on the client-side and I'm hoping to avoid the call requirement. I know in CouchDB you can do some basic modifications in their on-update handler but can't seem to find anything similar for Meteor/Mongo.

4

1 に答える 1

2

I finally found a nice approach to doing this whilst still using the minimongo interface on the client-side.

It seems you can intercept the insert and enrich the documents as part of the Deny policy, like this:

    myCollection.deny({
        insert: function(userId, doc) {
            doc.created = new Date().valueOf();
            doc.creator = userId;
            //other default fields
            return false;
        }
    });

I've tried to do similar with the update Deny policy to add a modified field but that doesn't seem to work the same way. As noted in the post linked above, this won't work for the Allow policy.

于 2012-12-18T15:29:50.003 に答える