Exception in callback of async function inside Collection.insert

Why does this get an error of “Exception in callback of async function: Error: Sample reason. [sample-error]”?

Units.insert(object, function(error, result){
     throw new Meteor.Error("sample-error", "Sample reason");
   return result;

Do note that this is in a method inside the server.

I don’t quite follow the question - from the small snippet you’re showing, this looks okay. It looks like your insert is failing, at which point your callback function is called with the error object populated, and throws the exception you’ve defined. Are you asking why it’s failing? If that’s the case we’ll need to see more code.

well if i don’t throw a new error it doesn’t get an exception in the server but the error will be a default 500 server error and i kinda wanna do custom errors. And it is hard to parse the error object in the client. I guess i have to try catch the exception in the server and throw my custom error?

Meteor.call("someMethod", function(error, result){
      //do something else

used in the client side

@mudum Did you ever resolve this? I am getting the same thing

@kalmanh : can we see some code, please?

There is a flaw in the OP’s code which will result in no data being returned to the Meteor.call - using an async callback in a method is only useful if you do not expect data to be returned (however, it should not result in an exception - so I expect there was some other issue).

I am using SimpleSchema, which automatically validates your inserts when you attach a schema.

I was getting the error above when throwing the error from inside the callback…

I ended up going with the following

` if(Meteor.isServer) {
Future = Npm.require(‘fibers/future’);
var myFuture = new Future();

Todos.upsert(todo._id, {$set: todo}
, function(error, result){

      var errorResults = error.invalidKeys;
      errorResults[0].errorType = true;
  // if(error){
  //   console.log("error %j", error.invalidKeys);
  //   // throw new Meteor.Error(error.invalidKeys);
  // }

return myFuture.wait();


I’m pleased you found a solution, but what I’m struggling to understand is why use a callback in the first place? On the server, you can write synchronous-style MongoDB calls, because Meteor wraps these for you.

So, this:

try {
  return Todos.upsert(todo._id, {$set: todo});
} catch (error) {
  throw new Meteor.Error('some', 'error message');

I will have to try that

I’m getting this error using the followig code, where I am using the Meteor.Error to throw a message back to the client:

'/save': async function(foobar) {

    return async someAsyncFunction(val, function(){

        // Do some stuff here...
        // Update database.
                _id: id,
                "some_id": someId
                $set: {"complete": 'Y'}
                    throw new Meteor.Error("method-failed");
                    throw new Meteor.Error("method-completed");

Any way how to address this?