0

I need help with my loop. I try to get data from Google Finance API. Google doesn't provide multi-tiker API so 1 request per 1 ticker. For 1 ticker, it's work well, but I need more than one. I try to do it in a loop but I get

undefined[object Object][object Object]

  .controller('currency', function ($scope, $http){
  var tickers =["EURUSD","AUDUSD]; 
    for (i = 0; i < 2; i++) {
   $http.get('https://www.google.com/finance/info?q=CURRENCY%3a'+tickers[i]).then(function successCallback(response) {
              $scope.currencydata = $scope.currencydata + JSON.parse(response.data.substr(3));


        })}
    })
3
  • $http.get is asynchronous Commented Feb 17, 2017 at 6:33
  • use $q.all, for all promises Commented Feb 17, 2017 at 6:34
  • 1
    I think there is a missing closing quotation mark in var tickers =["EURUSD","AUDUSD"]; Commented Feb 17, 2017 at 6:51

3 Answers 3

1

The JSON.parse method outputs an object. You cannot do a plus operation on two objects.

Change the following:

$scope.currencydata = $scope.currencydata + JSON.parse(response.data.substr(3));

To this:

$scope.currencydata.push(JSON.parse(response.data.substr(3)));

Make sure to initialize the $scope.currencydata variable before the loop.

$scope.currencydata = [];

Otherwise the .push method will not be available.

Sign up to request clarification or add additional context in comments.

Comments

0

Use $q.all in angular

Do some thing like

var functionsToFire = []
["EURUSD","AUDUSD].forEach(function(currncy){
   var hello = create http request function
   functionsToFire.push(hello)
})

$q.all(functionsToFire).then(function (result) {
 console.log(result)
});

AND ULTIMATELY IT SHOULD LOOK LIKE THIS

$q.all([request1function, request2function, request3function, request4function]).then(function(result){
    for (var i = 0; i < result.length; i++){
        theResults.push(result[i]);
    }
});

Comments

0

You forgot ". Try this:

 .controller('currency', function ($scope, $http){
      var tickers =["EURUSD","AUDUSD"]; 
        for (i = 0; i < 2; i++) {
       $http.get('https://www.google.com/finance/info?q=CURRENCY%3a'+tickers[i]).then(function successCallback(response) {
                  $scope.currencydata = $scope.currencydata + JSON.parse(response.data.substr(3));


            })}
        })

1 Comment

yea loss " when copy. Thx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.