I am having trouble getting a server Meteor.method to return a successful response when I wrap the return in a callback.
It works fine when not wrapped in a call back.
It can throw new Meteor.errors both when wrapped in a call back and when not wrapped in a call back.
Wrapping the Meteor.call on the client side in this exact same fashion works fine.
But, for some reason doing this on the server does not return a response value when return is called.
Here is an example. This is not my exact code but very close. It is very close though.
Meteor.methods({
insertData: insertData
});
function insertData(params){
validateParams(params, function(bool, msg){
if(bool){
//do stuff, like insert records
result = 'thanks a million gagillions';
console.log(result);
return result;
} else {
throw new Meteor.Error(513, msg);
}
});
}
validateParams(params, callback){
for (var key in params) {
value = params[key];
if(_.isEmpty(value) || _.isUndefined(value) || _.isNull(value)) {
callback(false, 'Please enter your "'+ key + '".');
return;
}
}
callback(true);
}
How do I get this to work so that the result is returned?
You don’t need callbacks here. You might find the straight-line code more readable.