简体   繁体   中英

Returning meteor call method

my first time with Meteor today :)

I made a simple form that makes a POST request to the Ruby API to return an auth_code

Meteor.call("serverEx", emailInput, passwordInput) works great and shows an successful return in the Meteor server.

So my problem is, I am trying to return that auth_code into a variable in the meteor client

console.log(finalVar) is not working, shows undefined.

Any ideas? Having a feeling, I missed out something really basic.

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");
         }
      });
    }

  });

}

Try with the callback option maybe.

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

I think the problem you are running into is synchronization. Typically, I would make the method call like this using the Meteor.call callback function:

Meteor.call("serverEx", emailInput, passwordInput, function(error, result){
    if (error)
       alert(error.reason)
    else
       finalVar = result;
});

Also, it looks like you aren't returning anything from your server-side method. Try this.

"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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM