Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
443 views
in Technique[技术] by (71.8m points)

Error when updating an object in auto-form with meteor

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...