Experimenting with Meteor due to this question, I came to the following conclusion:
Defined in a shared directory (client/server), this will throw an Reference Error:
if(Meteor.isServer) {
// could depend on server logic, this is not Meteor.isServer!
serverVar = true;
}
Meteor.methods({
myMethod: function() {
if(serverVar) {
return "secret";
} else {
throw Error();
}
}
}
Then, on the client:
Meteor.call("myMethod", function(err, res) {
console.log(res);
}
Leads to: ReferenceError: serverVar
But this code, being defined on the server side only, runs flawlessly:
// could depend on server logic, this is not Meteor.isServer!
serverVar = true;
Meteor.methods({
myMethod: function() {
if(serverVar) {
return "secret";
} else {
throw Error();
}
}
}
Note that I only switched to a server-side directory instead of a shared one and removed the if-clause.
I thought that both approaches should be equivalent, ignoring the fact that code is visible
on the client when only limited by Meteor.isServer
.
This leads me to the conclusion that Meteor, using the first approach, tries to run code on the client when it is not explicitly limited to the server. Is that true?! What could be another explanation?