简体   繁体   English

For循环在均值堆栈中导致Angularjs应用程序出现问题

[英]For loop causing issue on Angularjs app in a mean stack

I am having a strange problem where when i add a single line of code the app doesn't work after a refresh Here is my controller : 我遇到一个奇怪的问题,当我添加一行代码时,刷新后该应用程序无法正常工作这是我的控制器:

myApp.controller('messengernew', ['$scope', '$resource', 'AuthService', '$location', '$http', 'User','Message', function ($scope, $resource, AuthService, $location, $http, User, Message) {

$scope.users=[];
$scope.messages=[];


User.getAllUsers().then(function(users) {
    $scope.users = users;
    for (var i = 0; i < users.length; i++) {
      (function(user){
        console.log('username ' + user.username);
        // User.incrementReputation(user.username);
        Message.getUnreadMessages(user.username, localStorage.getItem("token")).then(function(messages) {

            $scope.messages.push(messages);

        })
      })(users[i]);
    }
})


$scope.username=localStorage.getItem("token");

$scope.gotomessages = function (text) {

          var contact =text;
          localStorage.setItem("contact", contact);  
          $location.path('/messenger');
    };

}]);

If i uncomment User.incrementReputation(user.username) the app doesn't work after a refresh can you help ? 如果我取消注释User.incrementReputation(user.username) ,则刷新后该应用程序无法正常工作,您可以帮忙吗?

And the function incrementReputation is in a service and here is the code : 函数crementReputation在服务中,下面是代码:

function incrementReputation(username) {

  getUser(username).then(function(users) {

      (function(user){

        $http.put('/api/incrementReputation', {}, {params:{id:user._id}});
      })(users[0]);

  })
} 

and the function getUser also in a service (the same as incrementReputation )) : 并且服务中的功能getUser也与服务相同(与crementReputation相同):

function getUser(username) {

           var Users = $resource('/api/users', {},{
           query: {method: 'get', isArray: true}
           });


         return Users.query({username:username}).$promise

          }

Shot in the dark with the given information. 使用给定的信息在黑暗中射击。 May be a semicolon issue in the getUser function. 可能是getUser函数中的分号问题。

return Users.query({username:username}).$promise;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM