【问题标题】:Returning meteor call method返回流星调用方法
【发布时间】: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


【解决方案1】:

也许可以试试回调选项。

 var finalVar;
     Meteor.call("serverEx", emailInput, passwordInput,function(err,result){
         if(!err){
              finalVar = result;
          }
      });      
          console.log(finalVar);

【讨论】:

  • 啊,你打败了我!
  • @RamsayLanier 啊哈 =p
【解决方案2】:

我认为您遇到的问题是同步。通常,我会使用 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;
}

【讨论】:

  • 更新了我的答案 - 如果这不起作用,请尝试不要在服务器端的 HTTP.call 中使用回调并直接将 httpResult 设置为调用结果。
  • 唯一的问题是,客户端在服务器完成响应之前返回值。如果我在 http.call 中尝试了回调,我必须把函数放在哪里?
  • 不错!这实际上非常有用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 2017-01-10
  • 1970-01-01
  • 2016-03-12
  • 2014-06-30
相关资源
最近更新 更多