Error when updating an object in auto-form with meteor

社会主义新天地 提交于 2019-12-25 06:36:29

问题


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:


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;
        }
    } 
}


来源:https://stackoverflow.com/questions/30668659/error-when-updating-an-object-in-auto-form-with-meteor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!