0
         // Check voted
            var votes = res.data.votes;

            if(votes.length == 0){$scope.like = true;}
            votes.forEach(function(vote){
                if(vote.userId === auth.profile.user_id) {
                    $scope.liked = true;
                } else {
                    $scope.like = true;
                }
            });

I have written a code to check if the user has voted or not, but I'm having a small issue with the else statement:

using the following code, the $scope.liked works correctly, but the else statement only goes for the first. How can I edit this, so he goes through all the votes, and if nothing is found, he displays the $scope.like

1 Answer 1

3

What about just do it outside the loop?

votes.forEach(function(vote){ if(vote.userId === auth.profile.user_id) { $scope.liked = true; } }); $scope.like = !$scope.liked;

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.