NiSu
NiSu

Reputation: 105

push is not a function Angularjs error

Im a starter in Angularjs and learn it now. I got a web application where i want to add a password to a list. The data came from a database. At the start of the project i got the data in an json file and not on the database. After i chaged (data on database) i got the following error.

i got the following code:

$scope.savePassword = function () {
    if ($scope.currentPassword.Id == null) {
        $scope.passwords.push($scope.currentPassword);
        $scope.isShownEdit = false;
    } else {
        var index = $scope.passwords.findIndex(function (item) {
            return item.Id == $scope.currentPassword.Id;
        });
        $scope.passwords[index] = $scope.currentPassword;
        $scope.isShownEdit = false;
    }
}

Here i got the data which should be saved.

<div>
     <!-- Name des Passwortes -->
     <div class="cClearFloat cInputSpace">
         <input placeholder="Name" maxlength="12" ng-model="currentPassword.Name">
     </div>
     <!-- Passwort des Passwortes -->
     <div class="cClearFloat cInputSpace">
         <input placeholder="Passwort" maxlength="12" ng-model="currentPassword.Passwort">
     </div>
     <!-- Beschreibung des Passwortes -->
     <div class="cClearFloat cInputSpace">
         <input placeholder="Beschreibung" maxlength="25" ng-model="currentPassword.Beschreibung">
     </div>
     <!-- Liste mit den kategorien -->
     <div class="cClearFloat cInputSpace">
         <div class="divSmall">
             <label class="inputDropdownPlaceholder">Kategorien</label>
                 <div class="divOptions" ng-repeat="item in categories.Items">
                     <label class="labelDropDown"><input type="checkbox" class="inputDropDown" ng-model="item.isChecked" ng-checked="checkedCategory(item)" ng-init="item.isChecked = checkedCategory(item)" ng-change="changedCategory(item)">{{item.Name}}</label><br>
                 </div>
         </div>
               <div class="cClearFloat cButtons">
                   <button class="cButtonSpeichern" ng-click="showAlertPassword()">Speichern</button>
                   <button class="cButtonAbbrechen" ng-click="isShownEdit = false">Abbrechen</button>
              </div>
     </div>
</div>

check if all fields got an input

    $scope.showAlertPassword = function () {
    if ($scope.currentPassword.Name && $scope.currentPassword.Passwort && $scope.currentPassword.Beschreibung && $scope.currentPassword.Category.length) {
        $scope.savePassword();
    } else {
        alert("Bitte füllen Sie alle Felder korrekt aus!", "Fehler");
    }
}

I implement the data from the database like this:

$scope.passwords = hateoasService.call('http://localhost:20670/passwords');

When i start the application and want to add the data with the "Speichern" click i get the error.

[error image]

I dont know why i get this error can someone help me?

Upvotes: 1

Views: 773

Answers (3)

Jigar7521
Jigar7521

Reputation: 1579

You have to first initialize it, then after you can push element into it like :

$scope.passwords = [] // in case of array, | do {} in case of json.

$scope.savePassword = function () {
    if ($scope.currentPassword.Id == null) {
        $scope.passwords.push($scope.currentPassword);
        $scope.isShownEdit = false;
    } else {
        var index = $scope.passwords.findIndex(function (item) {
            return item.Id == $scope.currentPassword.Id;
        });
        $scope.passwords[index] = $scope.currentPassword;
        $scope.isShownEdit = false;
    }
}

Upvotes: 0

Sravan
Sravan

Reputation: 18647

you used currentPassword instead of $scope.currentPassword,

Change currentPassword instead of $scope.currentPassword and you are done

Then you might be reffering $scope.current_password hash without declaring it.

initialize $scope.current_password = {} at your controller starting.

$scope.savePassword = function () {
    if ($scope.currentPassword.Id == null) {
        $scope.passwords.push($scope.currentPassword);
        $scope.isShownEdit = false;
    } else {
        var index = $scope.passwords.findIndex(function (item) {
            return item.Id == $scope.currentPassword.Id;
        });
        $scope.passwords[index] = $scope.currentPassword;
        $scope.isShownEdit = false;
    }
}

Upvotes: 1

charliebeckwith
charliebeckwith

Reputation: 1449

You are pushing currentPassword to the array when elsewhere in your code you are referencing $scope.currentPassword which are two unique references to objects.

Changing it to this should resolve your error: $scope.passwords.push($scope.currentPassword);

Upvotes: 0

Related Questions