Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor autoform, async callback in before hook

I am using Autoform and Slingshot for my S3 interaction. When the user submits the form, i want to intercept the process, upload the file to S3 through Slingshot, extend the doc object with the returned downloadUrl and then at that point, return the new updated doc, 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 url (however looks weird, C://fakepath/filename..) on the document, but fails to upload it to the S3 server

Also side question, why doesnt the console.log(doc); in the before hooks log anything to the client/server?

like image 895
Tarlen Avatar asked Jan 28 '15 09:01

Tarlen


1 Answers

I'm not familiar with auto form but I think your before hook is incorrect.

From https://github.com/aldeed/meteor-autoform#callbackshooks , it 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 with

method: function(doc, template)
like image 115
kp_ping Avatar answered Nov 06 '22 07:11

kp_ping