Meteor form, asynchronous callback before hook

I use Autoform and Slingshot for my S3 interaction. When the user submits the form, I want to intercept the process, upload the file to S3 via Slingshot, expand the doc object with the return downloadUrl , and then at that moment return a new updated document and continue the autoform process

I have the following code:

 {{#autoForm collection="Tabs" id="newTabForm" type="method" meteormethod="createTab"}} ... <div class="modal-body"> <fieldset> {{> afFormGroup name='downloadUrl' type='file' class='file-bag'}} ... AutoForm.hooks({ newTabForm: { before: { insert: function(doc, template) { console.log(doc); var file = $('.file-bag')[0].files[0]; var self = this; uploader.send(file, function(error, downloadUrl) { if (error) { throw new Meteor.Error(error); } doc = _.extend(doc, { downloadUrl: downloadUrl }); self.result(doc); }); } }, .... Meteor.methods({ createTab: function(doc) { check(doc, TabSchema); var priceInCents = doc.price * 100; var extraTabAttributes = { userId: Meteor.userId(), price: priceInCents }; _.extend(doc, extraTabAttributes); Tabs.insert(doc, function(error, result) { if (error) { return error; } }); } 

Which correctly stores the url (however it looks strange, C: // fakepath / filename ..) in the document, but it is not possible to upload it to the S3 server

Also a side question is why console.log(doc); in the list before it intercepts something on the client / server?

+6
source share
1 answer

I am not familiar with the automatic form, but I think that your hook is wrong.

From https://github.com/aldeed/meteor-autoform#callbackshooks , he said

 before: { // Replace `formType` with the form `type` attribute to which this hook applies formType: function(doc) {} } 

So in your case

 insert: function(doc, template) 

Should be replaced by

 method: function(doc, template) 
+3
source

Source: https://habr.com/ru/post/981653/


All Articles