簡體   English   中英

如何使用Meteor和React進行API調用

[英]How to make an API call using Meteor and React

我有這種流星方法:

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

還有一個在render函數中帶有按鈕的React Component.jsx,該按鈕調用如下函數:

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

問題是我該如何使用回調函數中的響應來呈現其內容。

提前致謝。

好吧,您可以做一個小變化,如下所示。 我沒有測試過,但我認為它應該可以工作。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM