Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return error from Meteor.methods function

Tags:

meteor

how to return error from Meteor.methods function? I call my function:

Meteor.call('checkCode', mycode, function(error,result){
    console.log(result['name']);
  })

And it returns me name of person whose "code" is the same as argument. But how return an error if there is nobody with input code? My function code:

checkCode: function(zcode){
    return Codes.findOne({code: zcode});
  }

Thanks!:)

like image 413
yeswecode Avatar asked Jan 01 '14 10:01

yeswecode


1 Answers

You can use throw as you would any normal javascript error. Meteor picks it up and returns the error to the client.

var code = Codes.findOne({code: zcode});

if(!code) 
    throw new Meteor.Error(500, 'Error 500: Not found', 'the document is not found');

return code;
like image 192
Tarang Avatar answered Oct 14 '22 17:10

Tarang