API factory errors


#1

Hi everyone!

I am trying to create a factory for an API weather forecast response but when I try to fill the list with the items in the array it says that is not defined.

Here is the factory

.factory('FullForecastService', function($q, $timeout, $http){
    $http.get('http://api.wunderground.com/------------------------------------------------.json')
        .success(function(data) {
            forecastfuture = [];
            console.log(data);
            for(i=0; i<36; i++) {
                var day = data.hourly_forecast[i].FCTTIME.pretty;
                var maxtemp = data.hourly_forecast[i].temp.metric;
                var mintemp = data.hourly_forecast[i].dewpoint.metric;
                var feelslike = data.hourly_forecast[i].feelslike.metric;
                var humidity = data.hourly_forecast[i].humidity;
                var windspd = data.hourly_forecast[i].wspd.metric;
                var winddir = data.hourly_forecast[i].wdir.dir;
                var sky = data.hourly_forecast[i].condition;
                var precip = data.hourly_forecast[i].qpf.metric;
                var snow = data.hourly_forecast[i].snow.metric;
                var icon = data.hourly_forecast[i].icon_url;
               forecastfuture.push({
                    id: i,
                    day: day,
                    maxtemp: maxtemp,
                    mintemp: mintemp,
                    feelslike: feelslike,
                    humidity: humidity,
                    windspd: windspd,
                    winddir: winddir,
                    sky: sky,
                    precip: precip,
                    snow: snow,
                    icon: icon});

            }
        }
    )

    return {
        all: function(){
            var deferred = $q.defer();
            $timeout(function(){
                deferred.resolve(forecastfuture);
            }, 1000);
            return deferred.promise;
        },
        allSync : function(){
            return forecastfuture;
        },
        get : function(singledayId){
            for(var i= 0, l=forecastfuture.length; i < l; i++){
                    if(forecastfuture[i].id == singledayId){
                        return forecastfuture[i];
                    }
            }
        }
    }})

here is the two controllers for the forecast list and the single day:

.controller('ForecastCtrl', function($scope, FullForecastService) {

        $scope.forecastfuture = [];
        //$scope.movies = MovieService.all();
        $scope.forecastfuture = FullForecastService.allSync();
    })

Also in the Single Forecast Controller the singleId in the line it is not working:

.controller('SingleForecastCtrl', function($scope, $stateParams, FullForecastService){
    $scope.singleforecast = FullForecastService.get($stateParams.singledayId);


})

Templates:
List

<ion-nav-view name="forecast"></ion-nav-view>
<ion-view view-title="36 Hr Weather Forecast">
    <ion-content class="background">
        <ion-list>
            <ion-item ng-repeat="singleday in forecastfuture" href="#/app/forecast/{{singleday.id}}">
                <img src={{singleday.icon}} />
                {{singleday.day}}
            </ion-item>
    </ion-content>
</ion-view>

Single Forecast

<ion-view view-title="Weather Forecast">
  <ion-content class="background">
    <h1>Forecast Day:</h1>
    <ion-list>
      <ion-item>
        {{forecastfuture.day}}
      </ion-item>
    </ion-list>
  </ion-content>
</ion-view>

The error that i get:


#2

you should define this variable before you use it…

.factory('FullForecastService', function($q, $timeout, $http) {
  var forecastfuture = [];
 ....