I'm getting error with that piece of code on Meteor.js :
Client side :
Meteor.call("logUser", function(myvar){
console.log("le chat client : "+myvar) ;
Session.set("chatId", myvar);
});
Server side :
Meteor.methods({
logUser : function(mycallback){
mycallback("myString");
}
});
I dont really understand why it doesnt work, the Method is well triggered but the code breaks when "mycallback" is called :
"Undefined is not a function"
You cannot pass functions as arguments to a Meteor method.
Why is that so ?
From a security concern, are you aware that basically you're trying to let the client execute any code in the server-side context ? You cannot trust the client, someone could use Meteor.call("logUser",function(){malicious code that ruins your server}); in the browser console and kill your server in one line of code !
I guess that parameters to Meteor.call are serialized using the JSON format, which doesn't allow functions, so this is not technically possible. You could pass the function as a string and eval it on the server but that would be a terrible mistake !
What happens here is that the function you pass to Meteor.call is indeed treated as a callback executed on the client after the asynchronous execution of the method has been performed on the server, so that's why you're tricked into thinking that the method is well triggered whereas it's obviously not the case.
So the meteor method isn't passed any parameters at all, that's why "callback" is undefined in its code.
Quoting the Meteor docs : (http://docs.meteor.com/#meteor_call)
"If you include a callback function as the last argument (which can't be an argument to the method, since functions aren't serializable), the method will run asynchronously."
So you're doing something wrong here (Session is not available on the server anyway), I think you should reconsider the approach to whatever you're trying to achieve.
Solution :
Client side :
Meteor.call("logUser", function(error , result){
if(error)
console.log(error.reason);
else
console.log(result);
});
Server side :
Meteor.methods({
logUser : function(){
return "myString";
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With