Skip to content
This repository has been archived by the owner on Mar 25, 2020. It is now read-only.

this.template is undefined when before hook is a function #45

Open
mbabauer opened this issue Sep 25, 2015 · 1 comment
Open

this.template is undefined when before hook is a function #45

mbabauer opened this issue Sep 25, 2015 · 1 comment

Comments

@mbabauer
Copy link

I am using cfs-autoform to create an attachments upload feature. I need to be able to "clean" my schema prior to submit via Meteor.call, but when I try to run CfsAutoForm.Hooks.beforeInsert(doc) I get the following error in the browser:

Uncaught TypeError: Cannot read property '$' of undefined
    Hooks.beforeInsert @ cfs-autoform-hooks.js:11
    AutoForm.hooks.insertAssignmentsForm.before.method @ assignments.js?8a262c80b842e07fc32509b0d2ce45b425685c40:6
    runHook @ autoform-events.js:241runBeforeHooks @ autoform-events.js:250
    AutoForm.addFormType.onSubmit @ method.js:15
    autoFormSubmitHandler @ autoform-events.js:326
    (anonymous function) @ template.js:463
    Template._withTemplateInstanceFunc @ template.js:437
    (anonymous function) @ template.js:462
    (anonymous function) @ view.js:869
    Blaze._withCurrentView @ view.js:523
    (anonymous function) @ view.js:868
    (anonymous function) @ events.js:47
    jQuery.event.dispatch @ jquery.js:4665
    jQuery.event.add.elemData.handle @ jquery.js:4333

When I placed a breakpoint on line 11 of cfs-autoform-hooks.js, the this has a template field but it's set to undefined. My Autoform hooks look like:

Template.assignments.onRendered(function() {
    AutoForm.hooks({
        insertAssignmentsForm: {
            before: {
                method: function(doc) {
                    CfsAutoForm.Hooks.beforeInsert(doc);

                    console.log('Cleaning doc: ', doc);
                    Assignments.simpleSchema().clean(doc);
                    console.log('doc: ', doc);

                    return doc;
                }
            },
            after: {
                method: function(err, result) {
                    if (err) {
                        console.log('An error occured: ', err);
                    } else {
                        console.log('Insert was successful: ', result);
                        method: CfsAutoForm.Hooks.afterInsert(err, result);
                    }
                }
            }
        }
    });
});
@mbabauer
Copy link
Author

Ok, so it looks like you can use call() to call a function and pre-set the this. So, my code now looks like:

Template.assignments.onRendered(function() {
    AutoForm.hooks({
        insertAssignmentsForm: {
            before: {
                method: function(doc) {
                    CfsAutoForm.Hooks.beforeInsert.call(this, doc);

                    console.log('Cleaning doc: ', doc);
                    Assignments.simpleSchema().clean(doc);
                    console.log('doc: ', doc);

                    return doc;
                }
            },
            after: {
                method: function(err, result) {
                    if (err) {
                        console.log('An error occured: ', err);
                    } else {
                        console.log('Insert was successful: ', result);
                        method: CfsAutoForm.Hooks.afterInsert(err, result);
                    }
                }
            }
        }
    });
});

which now seems to work. I still thing there is an issue with cfs-autoform-hooks.js that needs to be corrected, in that line 3 should not use this.template, but I don't know what is should be using in stead.

mbabauer pushed a commit to mbabauer/meteor-cfs-autoform that referenced this issue Sep 25, 2015
…rom a function rather than passed directly to the Autoform before hook (see issue at: aldeed#45)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant