2017-10-25 14 views
0

を来ている。これは、機能1私はreactJsに別の関数に一つの関数からパラメータを渡すのですが、結果がnull

import React, {Component} from "react"; 
import axios from 'axios'; 
import { config } from '../../config/config.js'; 
class Dashboard extends Component { 
    constructor(props) { 
    super(props); 
    } 
    componentWillMount(){ 
     var obj = { 
      campaign:campaignName, 
      campaignId:campaignId, 
      clientId:clientId, 
      clientName:clentName, 
      end:endDate, 
      start:startDate, 
      timeZone:new Date().getTimezoneOffset(), 
      ReportName:'Chargebacks', 
      widgetName:'Billing Cycle' 
    } 
    var resdata = ChartAPI.widgetApiCalls(config.apiUrl,obj); 
    console.log(resdata); 
    } 
} 

と私の最初のファイルであり、これは機能2

import axios from 'axios'; 
function charts(){ 
    this.widgetApiCalls = function(url,parmsobj){ 
     var byresspose=[]; 
     axios.get(url+"/reports",{params:parmsobj}) 
      .then(function(response){   
       for(var i in response.data){ 
        byresspose.push({"label":"Billing Cycle"+" "+response.data[i].billingCycle,"value":response.data[i].total}) 
       } 
       console.log(byresspose); 
      }); 
      return byresspose; 
     }; 
} 
charts = new charts(); 
module.exports = charts; 

と別のものですある関数1から別の関数2にパラメータを渡す正しい方法は何ですか?

+0

最初のファイルから 'charts'関数にデータを渡したいのですか? – Panther

+0

これをチェックしてください:[非同期呼び出しからの応答を返すにはどうすればいいですか?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-コール) –

+0

'./charts.js'からChartsAPIをインポートします。私はこの文を正確に追加します。 –

答えて

1

あなたのダッシュボードコンポーネントのソースファイルへのあなたのchartsモジュールをインポートする必要があります。

import ChartsAPI from './charts.js'; 

そして、あなたはcomponentWillMountにそれを呼び出すことができるようになります:

var chartsAPI = new ChartsAPI(); 
chartsAPI.widgetApiCalls(config.apiUrl,obj).then(function(result) { 
    var resdata = result; 
    console.log(resdata); 
}); 

これも返すようにwidgetApiCallsが必要です約束:

this.widgetApiCalls = function(url,parmsobj){ 
    return axios.get(url+"/reports",{params:parmsobj}) 
     .then(function(response){ 
      var byresspose=[];   
      for(var i in response.data){ 
       byresspose.push({"label":"Billing Cycle"+" "+response.data[i].billingCycle,"value":response.data[i].total}) 
      } 
      console.log(byresspose); 
      return byresspose; // this will come to the Dashboard `widgetApiCalls.then` 
     }); 
    }; 
+0

正確ですが、彼のコードの問題点とそれがなぜ修正されたのかについて少し詳しく説明することができます。具体的にどのように約束された遅延され、値は伝統的に – caesay

0

コールバック

約束

非同期&は私が最後のものを好む

を待っています。 その典型的な非同期問題です。そのほか

componentWillMount(){ 
      var obj = { 
       campaign:campaignName, 
       campaignId:campaignId, 
       clientId:clientId, 
       clientName:clentName, 
       end:endDate, 
       start:startDate, 
       timeZone:new Date().getTimezoneOffset(), 
       ReportName:'Chargebacks', 
       widgetName:'Billing Cycle' 
     } 
     this.getData(config.apiUrl,obj) 

     } 
     async getData(url, obj) => { 
      var resdata = await ChartAPI.widgetApiCalls(url,obj); 
      console.log(resdata); 

}

私はあなたが1つのモジュールとして定義されchartAPIする必要がないことだと思います。 コンポーネントで定義済み

+0

は動作しません –

関連する問題