title: Why can't I push to an array in an AngularJS factory?
body: <p>I have a service setup to get information from my API and append it to an array. But I keep getting an error message when I try to use the push() function.</p>
  
  <p>This is the factory:</p>
  
  <pre><code>.factory( 'SurgeryList', function($http, $rootScope) {
      var surgeryListService = {
          days: []
      };
  
      surgeryListService.getDays = function() {
          return days;
      };
  
      surgeryListService.nextPage = function() {
  
          var url = $rootScope.url+'/api/surgeries/day.json?surgery_date='+nextDate+'&amp;access_token='+$rootScope.accessToken+'&amp;callback=JSON_CALLBACK';
  
          $http.jsonp(url)
              .success(function(response, status, headers, config) {
                  days.push(response.day);
              }
          );
  
          return days;
      };
  
      return surgeryListService;
  })
  </code></pre>
  
  <p>I get this error for the push()</p>
  
  <pre><code>TypeError: days.push is not a function
  </code></pre>
  
  <p>I have tried to declare days as both an object and an array</p>
  
  <pre><code>days: {}
  /
  days: []
  </code></pre>
  
  <p>both give the same error.</p>
  
  <p>I also noticed that when I declare days as an array, then use console.log(days). It changes from an array into an object in the nextPage() function.</p>
  
  <p>I have also tried to replace push() with concat(), but that also gives me the "not a function" error.</p>
  
  <p>I can get this working if I used keys to add on the day objects.</p>
  
  <pre><code>days[response.day.date_string] = response.day;
  </code></pre>
  
  <p>but I want to use some angular filters which don't work properly when I set up days like that.</p>
  
  <p>edit:</p>
  
  <p>The working code was a slight alteration from pankajparkar's answer. I had to use a temp variable for the push, then concat that onto the service variable</p>
  
  <pre><code>var temp = [];
  temp.push(response.day);
  surgeryListService.days = temp.concat(surgeryListService.days);
  </code></pre>