Exception in callback of async function inside Collection.insert


#1

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

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

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


#2

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.


#3

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){
    if(error){
      //do something else
      console.log(error);
    }
}

used in the client side


#4

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


#5

@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).


#6

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){

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

return myFuture.wait();

}`


#7

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');
}

#8

I will have to try that


#9

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.
        SomeColl.update(
            {
                _id: id,
                "some_id": someId
            },
            {
                $set: {"complete": 'Y'}
            },
            function(err){
                if(err){
                    throw new Meteor.Error("method-failed");
                }
                else{
                    throw new Meteor.Error("method-completed");
                }
            }
        );
    });    
}

Any way how to address this?