1
someMethod = new ValidatedMethod({
    name: 'someMethodName',
    validate: new SimpleSchema({
        subId: {type: String, min:1},
        planId: {type: String}        
    }).validator(),
    async run(params){
        try{
                     //params is undefined
        } 

    }
});

Using async run(params) causes params to be undefined (seems like the context switches to Global context). Removing the async works fine (except that I cannot use await in the method body anymore obviously). Why is this, and how can I still use await inside a ValidatedMethod?

Note1 : I call the method from the client like so -- and get the same result if I try to use a regular Meteor.methods({}) definition. I am calling the method using Meteor.apply from the client

ClientHelpers.callWithPromise = function(methodName, methodArgs){
  //methodArgs must be an array
  return new Promise(function(resolve, reject){
    Meteor.apply(methodName, methodArgs, {wait:true}, function(error, result){
      if (error){
        reject(error);
      } 
      console.log(result);
      resolve(result);
    });
  });
}

Then, calling on client (am sure paramsObject is correct):

var myResult = await ClientHelpers.callWithPromise('someMethodName', [paramsObject]);

Note 2: I have also traced it through to the internals of Meteor.apply , where it is in fact sending the paramsObject over DDP, in debug session:

  // Sends the DDP stringification of the given message object
  _send(obj) {
    this._stream.send(DDPCommon.stringifyDDP(obj));
  }

Many thanks for any insight.

ASX
  • 635
  • 7
  • 18
  • 1
    Have you already tried the callpromise-mixin for ValidatedMethods? https://atmospherejs.com/didericis/callpromise-mixin – Stefan Neubert Jan 28 '19 at 14:24
  • Thanks, yes - the ClientHelpers.callWithPromise function in my code does the exact same thing. I believe the issue is using await in server-side code, when i need to be using Promise.await() as per https://github.com/meteor/meteor/issues/9473 . I don't quite understand why, but seems to help. – ASX Jan 29 '19 at 00:00

0 Answers0