Typechecking
While our method works there is one more thing we can learn in this area: how to type check our parameters.
For example, what if we want our parameter to be of the type Number
? Or perhaps only String
s are allowed.
To check this we can use a handy function provided by Meteor called check()
. Here's how we can use it:
both/collections/games.js
...
Meteor.methods({
gamesInsert: function(teamOneId, teamTwoId){
check(teamOneId, String);
check(teamTwoId, String);
var teamOne = Teams.findOne({_id: teamOneId});
var teamTwo = Teams.findOne({_id: teamTwoId});
...
Now if we try and do this in the console:
Meteor.call("gamesInsert", 1, 2, function(error, response){
console.log('error', error);
console.log('response', response);
});
We get a new error:
As you can see it's simply another Meteor.Error
, but with it's own error code and reason.