将收集到的API调用中的值从第一个方法传递到第二个相同对象中

问题描述:

我有一个包含两个方法的对象。第一个调用API并将response存储在变量中。将收集到的API调用中的值从第一个方法传递到第二个相同对象中

在第二种方法中,我使用this.nameOfFirstMethod()执行第一个方法,然后我想根据我在第一个方法中的API调用中收集的数字进行一些计算。

为了更清楚看看代码,开始在第二种方法阅读:

this.currencyConverter = { 
    getRatio: function(selectedCurrency) { 
     var selectedCurrency = selectedCurrency; 
     $http({ 
      url: 'http://api.fixer.io/latest?base='+selectedCurrency+'&symbols=PLN,CHF,EUR,USD,GBP', 
      method: 'GET' 
     }) 
     .then(function(response) { 
      var currentCurrency = { 
       toPLN: response.data.rates.PLN, 
       toCHF: response.data.rates.CHF, 
       toEUR: response.data.rates.EUR, 
       toUSD: response.data.rates.USD, 
       toUSD: response.data.rates.GBP 
      }; 
      console.log("Succesful store currentCurrency"); 
      return currentCurrency; 
     }, function(response) { 
      console.log("Problem occure while downloading money current currency!"); 
      console.log(response.data); 
     }); 
    }, 
    convertMoney: function(selectedCurrency,priceField) { 
     var priceField = priceField; 
     var selectedCurrency = selectedCurrency; 

     console.log('selectedCurrency in service: '+selectedCurrency); 
     console.log('priceField in service: '+priceField); 

     this.getRatio(selectedCurrency); 

     console.log(currentCurrency); 

     /* 
     var converted = { 
      PLN: function() { return priceField * $rootScope.currentCurrency.toPLN; }, 
      USD: function() { return priceField * $rootScope.currentCurrency.toUSD; }, 
      EUR: function() { return priceField * $rootScope.currentCurrency.toEUR; }, 
      CHF: function() { return priceField * $rootScope.currentCurrency.toCHF; }, 
      GBP: function() { return priceField * $rootScope.currentCurrency.toGBP; } 
     }; 
     */ 

    } 
} 

下面是相同的代码GIST如果有人不喜欢*的造型: https://gist.github.com/anonymous/e03de4de1af407bf70f4038acd77c961

请打开这个要点,因为我现在将解释基于特定线路。

所以在第30行我执行第一个方法。

在第9行中,我将检索到的数据存储在变量中,并在第17行中返回此数据(以便在第二种方法中使用它)。

最后我想console.log这在32行的第二个对象(现在只有console.log我以后会做我的数学)。

它不会与此return工作,在第二种方法的原因console.log线以下错误:

ReferenceError: currentCurrency is not defined

+0

到'this.getRatio调用(selectedCurrency)'是asyncronous,这意味着下面的代码不会等待它返回。 – NiVeR

你不getRatio的返回值赋值给一个变量 应该

currentCurrency = this.getRatio(selectedCurrency); 

而且您应该正确使用承诺。 所以将其更改为这样的事情(未测试)

this.currencyConverter = { 
    getRatio: function(selectedCurrency) { 
     var selectedCurrency = selectedCurrency; 
     return $http({ 
      url: 'http://api.fixer.io/latest?base='+selectedCurrency+'&symbols=PLN,CHF,EUR,USD,GBP', 
      method: 'GET' 
     }) 
     .then(function(response) { 
      var currentCurrency = { 
       toPLN: response.data.rates.PLN, 
       toCHF: response.data.rates.CHF, 
       toEUR: response.data.rates.EUR, 
       toUSD: response.data.rates.USD, 
       toUSD: response.data.rates.GBP 
      }; 
      console.log("Succesful store currentCurrency"); 
      return currentCurrency; 
     }, function(response) { 
      console.log("Problem occure while downloading money current currency!"); 
      console.log(response.data); 
     }); 
    }, 
    convertMoney: function(selectedCurrency,priceField) { 
     var priceField = priceField; 
     var selectedCurrency = selectedCurrency; 

     console.log('selectedCurrency in service: '+selectedCurrency); 
     console.log('priceField in service: '+priceField); 

     var currentCurrency = this.getRatio(selectedCurrency); 
     currentCurrency.then(res => console.log(res)); 

     //console.log(currentCurrency);   

    } 

}

+0

它改变了我的错误'TypeError:不能读属性'然后'未定义'让我补充说,这个错误在控制台出现在console.log(“成功的商店currentCurrency”);'看起来像'.then' isn工作不正常。 – BT101

+0

你在第一个函数中添加了返回$ http吗? – wheeler