[英]Returning meteor call method
我今天第一次在Meteor :)
我做了一个简单的表单,向Ruby API发出POST请求以返回auth_code
Meteor.call("serverEx", emailInput, passwordInput)
效果很好,并显示在Meteor服务器中成功返回。
所以我的问题是,我试图将auth_code返回到流星客户端中的变量中
console.log(finalVar)
无法正常工作,显示未定义。
有任何想法吗? 有一种感觉,我错过了一些基本的东西。
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");
}
});
}
});
}
尝试使用回调选项。
var finalVar;
Meteor.call("serverEx", emailInput, passwordInput,function(err,result){
if(!err){
finalVar = result;
}
});
console.log(finalVar);
我认为您遇到的问题是同步。 通常,我将使用Meteor.call回调函数进行如下方法调用:
Meteor.call("serverEx", emailInput, passwordInput, function(error, result){
if (error)
alert(error.reason)
else
finalVar = result;
});
另外,看起来您没有从服务器端方法返回任何内容。 尝试这个。
"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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.