Insert new doc via autoform hooks call meteor method

I want to insert a new document into db using autoform. Autoform hook calls a meteor method on server to insert the document.

I have this in template…

{{#autoForm collection="Reports" id="addReport" type="insert"}}
    <div class="row">
        <div class="col s6">
            {{> afQuickField name='hours'}}
        </div>
    </div>
    <button class="btn waves-effect waves-light modal-action modal-close"><i class="material-icons">save</i></button>
{{/autoForm}}

Then…

AutoForm.hooks({
    addReport: {
        onSubmit: function(insertDoc) {
            Meteor.call('addReport', insertDoc, function(error, result) {
                if (error) alert(error.reason);
            });
            return false;
        }
    }
});

then the method on server…

Meteor.methods({
    addReport: function(insertDoc) {   
        var report = _.extend(insertDoc, {
            userId: Meteor.userId(),
        });
        return Reports.insert(report);
    }
});

I have a createdAt and updatedAt fields in collection, but they all have autoValue thus, I believe no need to do insertion from client or in the meteor method.

So collection with schema looks like this:

Reports = new Meteor.Collection('reports');

Reports.attachSchema(new SimpleSchema({
    hours: {
        type: Number,
        label: "Number of hours",
        decimal: true
    },
    createdAt: {
        type: Date,
        label: "Created Date",
        autoValue: function() {
            if (this.isInsert) {
                return new Date;
            } else {
                this.unset();
            }
        },
        denyUpdate: true
    },
    updatedAt: {
        type: Date,
        autoValue: function() {
            if (this.isUpdate) {
                return new Date()
            }
        },
        denyInsert: true,
        optional: true
    },
    "userId": {
        type: String,
        autoform: {
            type: "hidden",
        }
    },
}));

When i run meteor, form displays, but submit does nothing. No visual cue as to if any error. No error message in both client and server console.

what am I doing wrong or missing?

why not just use type="method" instead of playing with hooks ?

I’ve tried this too:

{{#autoForm collection="Reports" id="addReport" meteormethod="addReport" type="insert"}}

then

Meteor.methods({
addReport: function(insertDoc) {   
    console.log('Add report was fired'); //added this part to know if it gets fired. It doesn't
    var report = _.extend(insertDoc, {
        userId: Meteor.userId(),
    });
    return Reports.insert(report);
}
});

then I commented out the hooks part.

Added the log part to know if the method gets called. No response in server console.

Still doesn’t submit

And when you try my suggestion from above ?

this should be…
{{#autoForm collection=“Reports” id=“addReport” meteormethod=“addReport” type=“method”}}

Your suggestion from above doesn’t do anything too.

Unfortunately, changing to type=“method”, still doesn’t call the method. I just don’t seem to find any example that works. I just don’t know.

autoform is giving me more troubles than helping. Dropping down to ordinary forms.

I’m having the same trouble today @seanmavley. I didn’t go to the trouble to do the Autoform.hooks() yet, just trying to make the type="method" meteormethod=addRecord work, and no luck. And no feedback from Autoform despite the fact that I’ve added Autoform.debug() to my client code.

Odd thing is that I have the editRecord method working like a charm, because I thought that was going to give me the most headaches, so I tackled it first. Now the “easier one” is the most trouble.

I see lots of references to setting up an autoform using collection="CollectionName" syntax, and when I did that for my forms autoform didn’t grab the schema and display the fields properly. When I use collection=Collections.CollectionName (without any quotes) it works great. Any idea what’s going on there?

1 Like