Http.post from server to client side returns undefined even with sleepForMs

Hi,

I have this code below wherein I result.data is undefined when returned to the client side. Hope you can suggest a work around for this. I tried adding the sleepForMs to delay the response but still the same the response is undefined.

Thanks in advance.

var server_link = https:// < link >

Meteor.methods({
editUser: function (userinfo) {
HTTP.post(server_link+‘update’, {
data: {
email: < email >,
companyemail: < company >,
name: < name >,
id: < id >
}
}, function(err, result) {

        Meteor._sleepForMs(2000);
        
        if(result.statusCode == 200){
            return {
                'status': true,
                'message': result.data
            }
        }else{
            return {
                'status': false,
                'message': err
            }
        }
    }
}

});

Your Meteor method is not returning anything. Because you provided a callback to the HTTP.post call it will run asynchronously on both the server and the client and returning something from a callback is meaningless without a handler, no matter how long you wait for.

The solution is to define your method on the server only and change the HTTP.post method to run synchronously. See this Example server method from the meteor docs:

Meteor.methods({checkTwitter: function (userId) {
  check(userId, String);
  this.unblock();
  try {
    var result = HTTP.call("GET", "http://api.twitter.com/xyz",
                           {params: {user: userId}});
    return true;
  } catch (e) {
    // Got a network error, time-out or HTTP error in the 400 or 500 range.
    return false;
  }
}});

going to try it now. thank you for the reply.

thanks just tried it and it worked. Changed the code to:

var server_link = https://link

Meteor.methods({
editUser: function (userinfo) {
var status = 0;
var response = HTTP.post(server_link+‘update’, {
data: {
email: < email >,
companyemail: < company >,
name: < name >,
id: < id >
}
}

    if(response) status = 1;

    return {
        status: 1,
        message: response
    }
}

});