Search code examples
meteormeteor-autoform

Meteor quick form method not fired


I have a meteor form in which i don't want the user to be able to modify the id, so i omit the field. however i add it back to be able to proceed the update. when i submit the form, it gets stuck and the server is never called..

any help appreciated, thanks!

donators.html:

<template name="dInfo">
  {{> dMove}}
  {{#if getDoc}}
    <label for="num">Numéro</label>
    <input type="text" id="num" class="form-control" placeholder="ID" value="{{getDoc._id}}" readonly>
    {{> quickForm schema=nwsc omitFields="_id" id="dUpt" doc=getDoc type="method" meteormethod="sayHi" buttonContent="Soumettre" buttonClasses="btn btn-lg btn-primary btn-block"}}
    <button type="button" class="btn btn-lg btn-primary btn-warning btn-block btn-del">Supprimer</button>
  {{/if}}
</template>

client side:

AutoForm.hooks({
  dUpt: {
    before: {
      method: function(doc) {
        doc._id = donators.find().fetch()[cursor.get()]._id;
      }
    },
    onError: function(i, e) {
      console.log(i);
      console.log(e);
    }
  }
});

server side:

Meteor.methods({
  sayHi: function(ins) {console.log(ins);},
  updateDonator: function(ins) {
    if(Meteor.userId() === null)
      return;
    if(ins._id === undefined)
      return;
    if(check(ins, dSchema) === false)
      return;

    dSchema.clean(ins);
    ins.closed = false;
    donators.update({_id: ins._id}, {$set: ins});
    logs.insert({user: Meteor.userId(), email: Meteor.user().username, table: 'Donators', action:'Update', item: ins._id, date: new Date()});
  }
});

Nothing is ever logged by sayHi, which is never called I suppose, and no error is ever fired.

Thanks!


Solution

  • According to the AutoForm documentation about callback hooks, in the before event you can modify the doc and return it by either:

    // Then return it or pass it to this.result()
    return doc; (synchronous)
    return false; (synchronous, cancel)
    this.result(doc); (asynchronous)
    this.result(false); (asynchronous, cancel)
    

    Your code does not return the modified doc. Add return doc; to the before -> method:

    AutoForm.hooks({
      dUpt: {
        before: {
          method: function(doc) {
            doc._id = donators.find().fetch()[cursor.get()]._id;
            return doc;  // Add this line
          }
        },
        onError: function(i, e) {
          console.log(i);
          console.log(e);
        }
      }
    });
    

    If AutoForm does not get a doc object back it will cancel the operation.