Server side async/await + check()

Hello,

I’m using meteor with check() and audit-check-arguments package.

When I use a meteor method using async/await and pass a parameter, even though I use check() to validate the function parametrs, the audit package still throws an exception indicating that not all input parameters have been checked. If I remove the async/await implementation, the package does not crib. What am I missing?

Example:

Meteor.methods({
    
    test: async function(param1){
          check(param1, String);
          ...
          await ....
    }
});

Throws an exception:

=> Client modified -- refreshing
I20200513-10:43:27.978(5.5)? Exception while invoking method 'test' Error: Did not check() all arguments during call to 'test'
I20200513-10:43:27.979(5.5)?     at ArgumentChecker.throwUnlessAllArgumentsHaveBeenChecked (packages/check/match.js:515:13)

Whereas this traditional meteor method does not throw any exceptions

Meteor.methods({
    
    test:  function(param1){
          check(param1, String);
          ...
 
    }
});

I know for sure that I am passing exactly one parameter.

You would normally expect to use a try/catch block when using async/await:

Meteor.methods({
    
    test: async function(param1){
      try {
          check(param1, String);
          ...
          await ....
      } catch(error) {
        throw new Meteor.Error(...);
      }
    }
});

@robfallows Why would you want to do this? This effectively voids the check IMHO.

@bharathkeshav12 I haven’t tried this, and it might not be the most elegant solution, but you could workaround the problem by deferring the call:


Meteor.methods({
    
    test: function (param1) {
          check(param1, String);
          ...
          Meteor.defer(async () => {
               await ....
          });
    }
});

But there are some caveats here: You might get errors if you call one of the Meteor-specific, fiber-based functions like Collection.find(), unless you wrap them with Meteor.bindEnvironment(). Also, deferring does not work in client-side method simulation, i.e. it only works in server-only methods.

Thanks.
I know I’m replying very late to this. I shall take a look at this when I switch back to that project.