1
I am trying to create an UPDATE on but is always appearing error message:
TypeError: Cannot read property 'length' of undefined
app.controller.js"
$scope.editTodo = function(usuario){
$scope.editedTodo = usuario;
var toDoTemplate = '<input type="text" ng-model="editedTodo.nome"></br><input type="tel" ng-model="editedTodo.tel"></br><input type="emal" ng-model="editedTodo.email"></br>'
var myPopup = $ionicPopup.show({
template: toDoTemplate,
title: 'Edite o contato',
subTitle: 'modificando o contato',
scope: $scope,
buttons: [
{ text: 'Cancelar' },
{
text: '<b>Salvar</b>',
type: 'button-positive',
onTap: function(e) {
if (!$scope.editedTodo.nome) {
//don't allow the user to close unless he enters wifi password
e.preventDefault();
} else {
for (var i = 0; i < $scope.usuarios.length; i++) {
if($scope.usuarios[i].id === $scope.editedTodo.id){
$scope.usuarios[i] = $scope.editedTodo;
return;
}
};
}
}
}
]
});
}
app.service.js
//UPDATE CADASTROS
this.editTodo = function(data){
return $http({
method:"POST",
url:Config.getUrl+"apiCadastro.php",
data:data,
headers:{
'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8;'
}
});
};
html profile.
<ion-view view-title="{{perfil}}">
<ion-nav-bar class="bar-balanced">
<ion-nav-back-button class="button-clear"></ion-nav-back-button>
</ion-nav-bar>
<ion-content ng-repeat="usuario in usuarioPerfil">
</br>
</br></br>
</br>
<div class="item item-text-wrap">
<div class="item item-text-wrap">
<div class="row row-center responsive-sm">
<div class="row row-center" style="text-align:center; padding: 0; margin:0;">
<div class="col col-center" style="background-color: grey; color: #fff; width: 100%; height: 100px; padding-top: 15px !important">
<h1 class="light">{{usuario.nome}}</h1>
<h4 class="light" style=" color: #fff;">USUÁRIO</h4>
</div>
</div>
</div>
</div>
</br>
<div class="item item-text-wrap">
<div class="row row-center responsive-sm">
<div class="row row-center" style="text-align:center; padding: 0; margin:0;">
<div class="col col-center" style="background-color: grey; color: #fff; height: 100px; padding-top: 15px !important">
<h1 class="light">{{usuario.tel}}</h1>
<p class="light" style=" color: #fff;">TELEFONE</p>
</div>
</div>
</div>
</div>
</br>
<div class="item item-text-wrap">
<div class="row row-center responsive-sm">
<div class="row row-center" style="text-align:center; padding: 0; margin:0;">
<div class="col col-center" style="background-color: grey; color: #fff; height: 100px; padding-top: 15px !important">
<h1 class="light">{{usuario.email}}</h1>
<p class="light" style=" color: #fff;">E-MAIL</p>
</div>
</div>
</div>
</div> </br> </br>
<a ion-button class="button button-full button-positive" ng-click="editTodo(usuario)">Editar</a>
</br>
</ion-content>
</ion-view>
Where is the $Scope.usuarios' array statement? The error is probably in the for line where it uses $Scope.usuarios.length . Make a check if( $Scope.usuarios ) before the for or put this part of your code here to see better.
– André Vicente
Thanks friend really I declared wrong, already managed to fix the mistake.
– soliney naiva