Is it possible to use destructuring in Meteor Methods with check?

When I call the method below from the client, the document is inserted successfully but I get an opaque 500 Error, details: undefined.

Here’s my Meteor Method:

// in lib/collections/things.js

newThing: function(thing, { optionalArray=[], optionalObject } = {}) {
  check(thing, {
   ...
  });
  check(optionalArray, [Match.Maybe({
     aString: String,
     aBoolean: Boolean
  )]);
  check(optionalObject, Match.Maybe(Object));

  // do stuff
  return Things.insert(thing)
});

If I don’t use destructuring, the error goes away so I’m thinking it has something to do with how destructured params are checked.

Appreciate any insight. :slight_smile:

The Meteor docs for Match.Maybe say (emphasis mine):

Matches either undefined , null , or pattern . If used in an object, matches only if the key is not set as opposed to the value being set to undefined or null . This set of conditions was chosen because undefined arguments to Meteor Methods are converted to null when sent over the wire.

And if you try to destructure null, you get a TypeError, the details of which aren’t sent to the client.
So (without seeing the real code) I think that’s what’s going on here

You could move the destructuring into the function body and test for null like so:

newThing: function(thing, options) {
  let optionalArray, optionalObject;
  if (options != null) ({ optionalArray = [], optionalObject }) = options;
  

But it might be easier to just skip the destructuring in that case

2 Likes