Reputation: 121
my first time with Meteor today :)
I made a simple form that makes a POST request to the Ruby API to return an auth_code
Meteor.call("serverEx", emailInput, passwordInput)
works great and shows an successful return in the Meteor server.
So my problem is, I am trying to return that auth_code into a variable in the meteor client
console.log(finalVar)
is not working, shows undefined.
Any ideas? Having a feeling, I missed out something really basic.
if (Meteor.isClient) {
Template.templateLogin.events({
'submit form': function(event) {
var emailInput = event.target.email.value;
var passwordInput = event.target.password.value;
var finalVar = Meteor.call("serverEx", emailInput, passwordInput);
console.log(finalVar);
return false;
}
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
});
/////////////////////
// METHODS
/////////////////////
Meteor.methods({
"serverEx" : function(a, b) {
var httpMethod = "POST";
var httpUrl = "http://xxxxxxx.herokuapp.com/signin";
HTTP.call(httpMethod, httpUrl, {'content-type': 'application/json; charset=utf-8', params: {
email: a,
password: b
}}, function (error, result) {
if (result.statusCode === 200) {
console.log("Success, the authcode is " + result.data.auth_token);
return result.data.auth_token;
}
if (result.statusCode === 401) {
console.log("Login failed, invalided email or password");
}
});
}
});
}
Upvotes: 0
Views: 368
Reputation: 426
I think the problem you are running into is synchronization. Typically, I would make the method call like this using the Meteor.call callback function:
Meteor.call("serverEx", emailInput, passwordInput, function(error, result){
if (error)
alert(error.reason)
else
finalVar = result;
});
Also, it looks like you aren't returning anything from your server-side method. Try this.
"serverEx" : function(a, b) {
var httpMethod = "POST";
var httpUrl = "http://xxxxxxx.herokuapp.com/signin";
var httpResult;
HTTP.call(httpMethod, httpUrl, {'content-type': 'application/json; charset=utf-8', params: {
email: a,
password: b
}}, function (error, result) {
if (result.statusCode === 200) {
console.log("Success, the authcode is " + result.data.auth_token);
httpResult = result.data.auth_token;
}
if (result.statusCode === 401) {
console.log("Login failed, invalided email or password");
}
});
return httpResult;
}
Upvotes: 1
Reputation: 11376
Try with the callback option maybe.
var finalVar;
Meteor.call("serverEx", emailInput, passwordInput,function(err,result){
if(!err){
finalVar = result;
}
});
console.log(finalVar);
Upvotes: 1