如何使用Meteor和React进行API调用

时间:2017-05-27 23:52:30

标签: javascript api reactjs meteor methods

我有这个Meteor方法:

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

还有一个React Component.jsx,它在render函数中有一个按钮,可以调用这样一个函数:

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

问题是如何使用回调函数中的响应来呈现其内容。

提前致谢。

1 个答案:

答案 0 :(得分: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});
            }
      });