Error when updating an object in auto-form with me

2019-09-10 06:23发布

I have checked the docs but I cannot get my head around this. I have an object that I want to update using Auto-Form and Collections2 with meteor.

//Schema

Records = new Mongo.Collection('records');

var Schemas = {};


Schemas.Record = new SimpleSchema({
title: {
    type: String,
    label: "Title",
    max: 200
},
caption: {
    type: String,
    label: "Caption",
    max: 200
},
text: {
    type: String,
    label: "Detailed text",
    optional: true,
    max: 1000
},
loc: {
    type: Object,
    optional: true,
    blackbox: true
},
createdAt: {
    type: Date,
    autoform: {
        type: "hidden"
    },
    autoValue: function() {
        if (this.isInsert) {
            return new Date;
        }
        else if (this.isUpsert) {
            return {
                $setOnInsert: new Date
            };
        }
        else {
            this.unset();
        }
    }
},
updatedBy: {
    type: String,
    autoValue: function() {
        return Meteor.userId();
    }
}
});

Records.attachSchema(Schemas.Record);

I have a hook so that I assign the object before update

AutoForm.hooks({
    insertCommentForm: {
        before: {
            insert: function(doc) {
                doc.commentOn = Template.parentData()._id;
                return doc;
            }
        } 
    },


    updateRecordForm: {
        before: {
            update: function(doc) {
                console.log("storing location data");
                doc.loc = Session.get('geoData');
                console.log(doc.loc);
                return doc;
            }
        } 
    }
});

I get this error.

Uncaught Error: When the modifier option is true, all validation object keys must be operators. Did you forget $set?

I don't know how to "$set" with autoform.

1条回答
等我变得足够好
2楼-- · 2019-09-10 06:46

when you are trying to update a document in Mongo, when you want to update only certain fields, you will use the $set modifier.

Records.update({ _id: 1 }, { $set: { loc: { lat: 12, lng: 75 } } })

The above would update only the loc value.

Records.update({ _id: 1 }, { loc: { lat: 12, lng: 75 } })

The above would remove all other keys and the record will only have the _id and the loc.

Your hook will have to set the loc key in doc.$set.

Please update your hook with the following code and it should work:

updateRecordForm: {
    before: {
        update: function(doc) {
            console.log("storing location data", doc);
            doc.$set.loc = Session.get('geoData');
            return doc;
        }
    } 
}
查看更多
登录 后发表回答