[SOLVED] Can't figure out how to call a method and wait for success/error before resetting my form's state


#1

I can’t figure out the best practice to call a method and wait for success/error before resetting my form’s state…

let documentToInsert = {
// some object
} ;

  //1. call Meteor method and pass in the document
  Meteor.call('Opportunity.insert', documentToInsert, function(error, result){
              // 2. if error....
        if (error) {Bert.alert('There was an error: ' + error, 'error'); return}
              // 3. if all good, fire bert-alert to let user know the form submitted
        Bert.alert('procurement opportunity added!', 'success');
            // 5. call onFormSubmit, which changes states and closes form... it works outside of meteor.call of course but not inside
        this.props.onFormSubmit();
  });

Exception in delivering result of invoking 'Opportunity.insert': TypeError: 'onFormSubmit' is not a function

#Solution:

_submitNewOpportunity(){

    const successfulSubmit = ( ) =>{
      this.props.onFormSubmit( );  
    }


    let documentToInsert = {
    // some object
    } ;


      Meteor.call('Opportunity.insert', documentToInsert, function(error, result){
        if (error) {
             Bert.alert('Oops! ' + error.reason, 'error'); 
              return
         }
        Bert.alert('procurement opportunity added!', 'success');
        successfulSubmit();
      });
}