-1

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

enter image description here

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)
            }
0

1 Answer 1

1

This happens because of the two way binding provided with angularJs . Which complicates situation like yours .

Coming down to your case in development mode. For the two way bound variables in angularjs , it varifies 10 times to make sure the variable retains the same value . That's what the watcher implies. It make sure of that if something is not changing too frequently otherwise because of two way data bound, it will go in an infinite loop updating view and js object. Seems for some reason it thinks you user value can change ( Not sure why ) may be the get request to persona can give random different values .

i would suggest to use the second approach for you . Again it might be printing twice in console for same reason but if you use it in view it should work properly.

In general also working with angularjs i think you should get the object to js 1st before updating the view

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.