Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...