返回流星呼叫方法
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;
}
相关文章:
- electronic BrowserWindow的最小高度和宽度在hide()show()方法之后不起作用
- 有没有任何方法可以将控制器从文件加载到ui路由器$stateProvider中
- 数组在递归方法中设置为null
- 打破承诺链的好方法是什么
- 在使用Polymer'加载所有json文件后执行方法;s的核心ajax
- 使用“;这个“;JavaScript原型方法中的关键字
- 序列化数据属性中对象的最可靠方法
- 返回流星呼叫方法
- 角度方法在服务呼叫后未在控制器中触发
- I'm在一些函数周围传递一个元素;这个“;关键字和“;呼叫“;方法
- 未捕获的类型错误:对象#<LanPartys>没有方法'呼叫'
- 呼叫控制器's方法
- Locu节点类型错误:无法调用方法'呼叫'的未定义
- Backbone.js listen要发布,Object[Object Object]没有方法'呼叫'
- 有没有一种方法可以听到流行音乐隐藏被呼叫
- 未捕获类型错误:对象“function()”没有方法'呼叫'
- 呼叫“;私人的“;方法来自“;“父”;对象
- jquery validate accept方法-TypeError:无法读取属性'呼叫'的未定义
- 呼叫号码方法与两个点在javascript
- 未捕获的类型错误:无法调用方法'呼叫'的未定义-下划线