Ionic application error: array.push() is not a fun

2019-09-15 16:55发布

问题:

I created an application where I have controller and factory. I have an array inside of the factory where I want to push id of the element to this array. However, when I am trying to push element to array I got an error that

"favorites.push is not a function"

Below you can find my controller and factory. Thank you for reading: Factory:

.factory('favoriteFactory',['$resource', 'baseURL','$localStorage', function ($resource, baseURL, $localStorage) {
            var favFac = {};
            var favorites = $localStorage.get('favorites', []);
          favFac.addFavorites = function (index) {
            for(var i=0; i<favorites.length; i++){
              if(favorites[i].id == index)
                return
            }
              favorites.push({id: index});
              $localStorage.storeObject('favorites',favorites)
          }
          favFac.deleteFromFavorites = function (index) {
            for (var i = 0; i < favorites.length; i++) {
              if (favorites[i].id == index) {
                favorites.splice(i, 1);
              }
            }
            $localStorage.storeObject('favorites', favorites)
          };

          favFac.getFavorites = function () {
            return $localStorage.getObject('favorites',[]);
          };
          return favFac
        }])

Controller:

    .controller('MenuController', ['$scope', 'menuFactory', 'favoriteFactory','baseURL', '$ionicListDelegate', 'dishes', '$localStorage',
         function($scope, menuFactory,favoriteFactory, baseURL, $ionicListDelegate, dishes, $localStorage) {


        $scope.baseURL = baseURL;
        $scope.tab = 1;
        $scope.filtText = '';
        $scope.showDetails = false;
        $scope.showMenu = true;
        $scope.message = "Loading ...";
        $scope.addFavorite = function (index) {
          console.log("index:" +index);
          favoriteFactory.addFavorites(index);
          $ionicListDelegate.closeOptionButtons();

        };
        $scope.dishes = dishes;

            $scope.select = function(setTab) {
              $scope.tab = setTab;

              if (setTab === 2) {
                $scope.filtText = "appetizer";
              }
              else if (setTab === 3) {
                $scope.filtText = "mains";
              }
              else if (setTab === 4) {
                $scope.filtText = "dessert";
              }
              else {
                $scope.filtText = "";
              }
                };
   $scope.isSelected = function (checkTab) {
      return ($scope.tab === checkTab);
    };

    $scope.toggleDetails = function() {
      $scope.showDetails = !$scope.showDetails;
    };
  }])

回答1:

I assume you are using ngStorage. The get method does not have a second parameter. Therefore, your attempt at returning a default value of [](empty array) is simply returning undefined and then you are attempting to push to undefined and not to an array.

The source code for ngStorage shows no second parameter for get: https://github.com/gsklee/ngStorage/blob/master/ngStorage.js

So this line:

var favorites = $localStorage.get('favorites', []);

Should be this:

var favorites = $localStorage.get('favorites') || [];