2017-05-27 27 views
0

我有這個流星方法:如何使用流星進行API調用和響應

Meteor.methods({ 

'RESTcall':function() { 
     this.unblock(); 
     return Meteor.http.call("GET", "http://www.viaggiatreno.it/viaggiatrenonew/resteasy/viaggiatreno/soluzioniViaggioNew/228/458/2017-05-31T00:00:00");}}); 

並與在渲染功能的按鈕調用這樣的功能的陣營Component.jsx:

search(){ 
     Meteor.call("RESTcall", (error, response)=>{ 
       console.log(response); //this works 
       this.setState({results: response}); //this throws an exception 
      } 
     }); 
} 

問題是我能做些什麼來使用回調函數來回報它的內容。

在此先感謝。

+0

在你的代碼中你使用了'console.log(results)',但是沒有'results'定義在任何地方。 'console.log(response)'的輸出是什麼?拋出什麼樣的異常? – tomsp

+0

對不起是console.log(響應)的作品,它是一個JSON響應 – dibbi

+0

你能提供例外嗎?你的電話似乎很好。 This.setState應該工作,因爲你正在使用一個箭頭函數。我想這個問題來自http調用,也許是一個CORS問題。 –

回答

0

那麼你可以做一個小的改變,下面給出。我沒有測試過,但我認爲它應該起作用。

search(){ 
     var that = this; /*Since the setState method in this is accessible here*/ 
     Meteor.call("RESTcall", (error, response)=>{ 
       console.log(response); 
       that.setState({results: response}); 
      } 
     }); 
+0

他正在使用一個箭頭函數,所以這應該綁定的反應類和this.setState應該工作。 –