Meteor autoform,钩前异步回调

Tar*_*len 6 meteor meteor-slingshot

我正在使用Autoform和Slingshot进行S3交互.当用户提交表单时,我想拦截进程,通过Slingshot将文件上传到S3,doc使用返回的对象扩展对象,downloadUrl然后返回新的更新文档,并继续自动化过程

我有以下代码:

{{#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; }
  });
}
Run Code Online (Sandbox Code Playgroud)

哪个在文档上正确存储url(但看起来很奇怪,C:// fakepath/filename ..),但无法将其上传到S3服务器

还有一个问题,为什么console.log(doc);在挂钩之前没有将任何东西记录到客户端/服务器?

kp_*_*ing 3

我不熟悉自动表单,但我认为你的 before 钩子不正确。

来自https://github.com/aldeed/meteor-autoform#callbackshooks,它说

before: {

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

}
Run Code Online (Sandbox Code Playgroud)

所以就你而言,

insert: function(doc, template)
Run Code Online (Sandbox Code Playgroud)

应替换为

method: function(doc, template)
Run Code Online (Sandbox Code Playgroud)