Lombric
Lombric

Reputation: 840

Meteor Async Method

I'm trying to call methods, in this one, I do an http get, if result is OK I return it and track it on my mongodb base, if it returns me an error, I want to track it too.

Unfortunately, it doesn't work ! I read posts on stackoverflow but there are only old issues.

Have you got any solution ?

Client :

Meteor.call('get',function(err, response) {
  console.log(err+" ee"+response);
});

Server :

var header = 'xxxxxxxx';
Meteor.startup(function () {

  Meteor.methods({
    get : function(){
      console.log("call");
      var url = 'http://xxxxxxxxxx';
      this.unblock();

      Meteor.http.get(url, function(err,res){
        if(!err){
          //tracking
          return res;
        }else{
          //tracking
          return err;
        }
      });  
    }
  });
});

Upvotes: 0

Views: 202

Answers (1)

user3374348
user3374348

Reputation: 4101

On the server, you can call HTTP.get without a callback to do a "synchronous" HTTP call. You need to do meteor add http at the command line to add HTTP to your project.

Meteor.methods({
  get: function(){
    console.log("call");
    var url = 'http://xxxxxxxxxx';
    this.unblock();

    try {
      var res = HTTP.get(url);
      // tracking
      return res;
    } catch (err) {
      // tracking
      return err; // or throw new Meteor.Error(...)
    }
  }
});

Upvotes: 1

Related Questions