I have been working on a webapp which will show forum topics. Now my problem is that I can't show user name on every topic reply.
ERD
Now the important part of this is persona(users) is related with respuestas (replys) through id_persona.
I'm trying to get users information like (name, lastname which are in persona database), So I have using some solutions.
HTML
<h2>{{titulo}}</h2>
<p>{{texto}}</p>
<div class="row">
<div class="container" ng-repeat="respuesta in respuestas">
<!-- solution 1 -->
<span>{{ getPersona(respuesta[3]) }}</span>
<!-- solution 2 -->
<div ng-init="searchUser(respuesta[3])">
<span ng-repeat="user in users" ng-if="respuesta[3] == user[0]">{{user[0]}}</span>
</div>
<p>{{respuesta[1]}}</p>
</div>
</div>
Titulo and texto are part of my tema (topic) which I got with $routeParams.
ng-repeat is getting every reply which is associated with this topic
now, solution 1 is <span>{{ getPersona(respuesta[3]) }}</span> where in my controller I have a function that get respuesta[3] (which is id_persona).
$scope.getPersona = function(persona) {
var url3 = 'http:/WEBSERVICE/persona?filter=id,eq,' + persona;
$http.get(url3).then( function(response) {
//console.log(response);
var user = response.data.persona.records[0];
console.log(user)
return user;
});
}
This should return an array with user information, but I'm getting an error
10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: []
net::ERR_INSUFFICIENT_RESOURCES
Second solution was <div ng-init="searchUser(respuesta[3])"> which also send respuesta[3] to function searchUser. this solution is creating an array(users) which has the information of users who reply on the topic and then with nf-if filter the one where respuesta[3] = user.id.
$scope.users = []
$scope.searchUser = function(persona) {
var url3 = 'https://WEBSERVICE/persona?filter=id,eq,' + persona;
$http.get(url3).then( function(response) {
var user = {
id: response.data.persona.records[0][0],
nombres: response.data.persona.records[0][3],
apellidos: response.data.persona.records[0][4] + ' ' + response.data.persona.records[0][5]
};
//console.log(user)
if ($scope.users.indexOf(user) == -1) {
$scope.users.push(user);
console.log($scope.users.indexOf(user));
console.log($scope.users)
}
})
}
What is the problem with this?
I don't my users get duplicate in the array Users that why I'm using $scope.users.indexOf(user)
I think solution 1 should be the one which work faster but it doesn't work and solution 2 works, but I'm getting duplicate user, also a try to do a for, but I didn't understand how it works, I did this:
for (var i = 0; i < $scope.users.length; i++) {
if($scope.users[i].id === users.id) {
console.log('ya existe');
} else {
$scope.users.push(user);
}
console.log($scope.users)
}
