[英]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.