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

CfsAutoForm.Hooks.beforeInsert() is called, but after my Meteor.call() to save the form #51

Closed
mbabauer opened this issue Oct 19, 2015 · 3 comments

Comments

@mbabauer
Copy link

I am using the method form type with CFS AutoForm. When I submit my form, the before hook is called, where I call CfsAutoForm.Hooks.beforeInsert(), but the lines that actually set the saved fileIds to the doc are not called until after my Meteor.call() executes and saves the doc. As a result, all my file uploads store the files, but the file ids are not properly set on the doc. I am providing my code below:

Form hooks:

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

                    doc = Assignments.simpleSchema().clean(doc);
                    return doc;
                }
            },
            after: {
                method: function(err, result) {
                    console.log('err: ', err);
                    console.log('result', result);
                    if (err) {
                        console.log('An error occured: ', err);
                    } else {
                        method: CfsAutoForm.Hooks.afterInsert.call(this, err, result);

                        FlowRouter.go('/assignments/' + result.assignmentId);
                    }
                }
            },
            onError: function(formType, error) {
                console.log('error: ', error);
            }
        }
    });
});

Form setup:

{{#autoForm collection="Assignments" id="insertAssignmentsForm" type="method" meteormethod="createNewAssignment"}}

Versions:

accounts-base@1.2.1
accounts-password@1.1.3
alanning:roles@1.2.14
aldeed:autoform@5.7.1
aldeed:autoform-bs-button-group-input@1.0.2
aldeed:autoform-bs-datepicker@1.1.1
aldeed:autoform-select2@2.0.2
aldeed:collection2@2.5.0
aldeed:simple-schema@1.3.3
aldeed:template-extension@3.4.3
amplify@1.0.0
anti:i18n@0.4.3
arillo:flow-router-helpers@0.4.5
autoupdate@1.2.3
babel-compiler@5.8.24_1
babel-runtime@0.1.4
babrahams:editable-json@0.6.0
babrahams:temple@0.3.3
base64@1.0.4
binary-heap@1.0.4
blaze@2.1.3
blaze-html-templates@1.0.1
blaze-tools@1.0.4
boilerplate-generator@1.0.4
caching-compiler@1.0.0
caching-html-compiler@1.0.2
callback-hook@1.0.4
cfs:access-point@0.1.49
cfs:autoform@2.2.1
cfs:base-package@0.0.30
cfs:collection@0.5.5
cfs:collection-filters@0.2.4
cfs:data-man@0.0.6
cfs:file@0.1.17
cfs:filesystem@0.1.2
cfs:graphicsmagick@0.0.18
cfs:http-methods@0.0.30
cfs:http-publish@0.0.13
cfs:power-queue@0.9.11
cfs:reactive-list@0.0.9
cfs:reactive-property@0.0.4
cfs:standard-packages@0.5.9
cfs:storage-adapter@0.2.2
cfs:tempstore@0.1.5
cfs:upload-http@0.0.20
cfs:worker@0.1.4
check@1.0.6
chuangbo:cookie@1.1.0
codechimp:autoform-masked-input@0.0.1
coffeescript@1.0.10
constellation:console@1.2.1
constellation:session@0.3.2
constellation:subscriptions@0.3.1
constellation:velocity@0.3.4
cosmos:browserify@0.8.1
dburles:mongo-collection-instances@0.3.4
ddp@1.2.2
ddp-client@1.2.1
ddp-common@1.2.1
ddp-rate-limiter@1.0.0
ddp-server@1.2.1
deps@1.0.9
diff-sequence@1.0.1
ecmascript@0.1.5
ecmascript-collections@0.1.6
ejson@1.0.7
email@1.0.7
fastclick@1.0.7
flyandi:is@1.0.2
force-ssl@1.0.6
geojson-utils@1.0.4
gwendall:session-json@0.1.7
hot-code-push@1.0.0
html-tools@1.0.5
htmljs@1.0.5
http@1.1.1
ian:accounts-ui-bootstrap-3@1.2.82
id-map@1.0.4
jquery@1.11.4
juliancwirko:s-alert@3.1.1
kadira:blaze-layout@2.1.0
kadira:flow-router@2.7.0
lai:collection-extensions@0.1.4
launch-screen@1.0.4
less@2.5.0_3
livedata@1.0.15
localstorage@1.0.5
logging@1.0.8
manuel:reactivearray@1.0.5
meteor@1.1.9
meteor-base@1.0.1
meteorhacks:aggregate@1.3.0
meteorhacks:collection-utils@1.2.0
meteorhacks:fast-render@2.10.0
meteorhacks:inject-data@1.4.1
meteorhacks:picker@1.0.3
minifiers@1.1.7
minimongo@1.0.10
mobile-experience@1.0.1
mobile-status-bar@1.0.6
momentjs:moment@2.10.6
mongo@1.1.2
mongo-id@1.0.1
mongo-livedata@1.0.9
mrt:mask@0.0.1
natestrauser:font-awesome@4.4.0
npm-bcrypt@0.7.8_2
npm-mongo@1.4.39_1
observe-sequence@1.0.7
ordered-dict@1.0.4
package-version-parser@3.0.4
practicalmeteor:chai@2.1.0_1
practicalmeteor:loglevel@1.2.0_2
promise@0.5.0
raix:eventemitter@0.1.3
raix:ui-dropped-event@0.0.7
rajit:bootstrap3-datepicker@1.4.1
random@1.0.4
rate-limit@1.0.0
reactive-dict@1.1.2
reactive-var@1.0.6
reload@1.1.4
retry@1.0.4
routepolicy@1.0.6
sanjo:long-running-child-process@1.1.3
sanjo:meteor-files-helpers@1.2.0_1
sanjo:meteor-version@1.0.0
service-configuration@1.0.5
session@1.1.1
sha@1.0.4
simple:json-routes@1.0.4
spacebars@1.0.7
spacebars-compiler@1.0.7
srp@1.0.4
standard-minifiers@1.0.1
stylus@2.511.0_2
templating@1.1.4
templating-tools@1.0.0
tracker@1.0.9
tsega:bootstrap3-floating-labels@3.1.0_1
twbs:bootstrap@3.3.5
ui@1.0.8
underscore@1.0.4
url@1.0.5
velocity:chokidar@1.2.0_1
velocity:console-reporter@0.1.3
velocity:core@0.10.5
velocity:html-reporter@0.9.0
velocity:meteor-internals@1.1.0_7
velocity:shim@0.1.0
velocity:source-map-support@0.3.2_1
webapp@1.2.2
webapp-hashing@1.0.5
xolvio:cucumber@0.14.10
zimme:active-route@2.3.2

Why is the before not finishing prior to the Meteor.call() call runs?

@mbabauer
Copy link
Author

After repeatedly looking at this, I wanted to share my findings with everyone so that I might get some help.

I started by putting breakpoints in the following places (I am using Chrome, if that matters):

  1. Lines 6 and 9 of my AutoForm hooks code above (on the CfsAutoForm.Hooks.beforeInsert and on the return line.
  2. In cfs_autoform.js lines 123, 127, 134, and 137 (increment doneFiles, increment failedFiles, call to deepFind, and call to deepSet. This is where I believe the file IDs are set back to the document

I then modified my Meteor.method to write out the document to console.log like so:

Meteor.methods({
    createNewAssignment: function(doc) {
        console.log('>>>> creating: ', doc);

        // Clean the doc to make sure any autoValues are done
        Assignments.simpleSchema().clean(doc);

        // Check the schema
        check(doc, Assignments.simpleSchema());

        doc._id = Assignments.insert(doc);

        //... do some other stuff

        return doc;
    },

Here are the chain of events that occur:

  1. Form is submitted
  2. Break on line 6 of AutoForm.hooks: CfsAutoForm.Hooks.beforeInsert.call(this, doc);
  3. Call to line 9 of AutoForm.hooks: return doc;
  4. Note: At this point, doc has no fileIds set
  5. This repeats like 3 or 4 times (break on line 6 followed by break on line 9 of AutoForm.Hooks)
  6. Server console writes out my console.log for the ">>>> creating: ...", but doc does not contain any fileIds
  7. File save output shows up in Server logs (I have debug turned on) indicating the files were finally saved
  8. Finally, after the Meteor.method ran in step 6 and the files are saved from step 7, I get a break on line 123 of cfs_autoform.js, followed by line 134, which again repeats a few times

I am not sure why the form seems to submit more than once, and that might possibly be the cause of all of this. However, what I can tell is that the cb function defined on line 121 of cfs_autoform.js does not run until after the Meteor.method is called to save the form values, and hence the fileIds are not set on the object. It seems like that cb method is getting called sometime later and so it's happening out of order with the rest of the process?

@mbabauer
Copy link
Author

Ok, further information...it looks like the fileIds are being set, but when the DDP request is sent over the filelds array is empty again. I am investigating why this might be, but I have a console.log right after I do clean(), and the fileIds are set as expected.

I am not sure why I saw issues with the timing. Best I can come up with is that my breakpoints might have caused the timing of the code to be off?

@mbabauer
Copy link
Author

I figured things out. You can't call CfsAutoForm.Hooks.beforeInsert and CfsAutoForm.Hooks.afterInsert the way I was within your own hook. You have to define them as such:

Template.assignments.onRendered(function() {
    AutoForm.hooks({
        insertAssignmentsForm: {
            before: {
                method: CfsAutoForm.Hooks.beforeInsert
            },
            after: {
                method: CfsAutoForm.Hooks.afterInsert
            },
            onError: function(formType, error) {
                console.log('error: ', error);
            },
            onSuccess: function(formType, error) {
                FlowRouter.go('/assignments/' + result.assignmentId);
            }
        }
    });
});

When you do it the way I was prior using call() you end up getting VERY funny results.

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