2014-05-19 68 views
0

我想調用方法,在這一個,我做一個http獲取,如果結果是好的我返回它並跟蹤它在我的mongodb基地,如果它返回一個錯誤,我想跟蹤它也。流星異步方法

不幸的是,它不工作!我閱讀了stackoverflow上的帖子,但只有老問題。

你有什麼解決方案嗎?

的客戶:

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

服務器:

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

回答

1

在服務器上,你可以調用HTTP.get沒有回調做一個 「同步」 HTTP調用。您需要在命令行上執行meteor add http以將HTTP添加到您的項目中。

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(...) 
    } 
    } 
}); 
+0

添加我不需要更改我的客戶電話? – Lombric

+0

你不需要改變你的客戶電話。 – user3374348