【发布时间】:2015-02-17 21:18:17
【问题描述】:
我今天第一次使用 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");
}
});
}
});
}
【问题讨论】:
-
你成功了吗?
-
我已经完全按照下面的方式尝试了回调,但仍然返回 undefined。
-
看起来我必须延迟方法调用,直到收到表单提交的完整响应?类似 wrapAsync 但我不知道如何包装它?任何建议
-
哦,“serverEx”方法实际上也没有返回任何东西。尝试将服务器端 HTTP 调用的结果存储在变量中并返回。我已经更新了我的答案。
标签: javascript node.js meteor