Javascript承诺链接(Javascript promise chaining)

编程入门 行业动态 更新时间:2024-10-26 07:36:22
Javascript承诺链接(Javascript promise chaining)

我很难理解如何将承诺联系起来。 我正在为我的应用程序编写登录功能,对Loopback的Angular SDK进行了改进。 验证用户凭据后,目标是确认用户的帐户处于活动状态,然后获取一些其他属性(包括用户的角色),并在用户具有管理员权限时将标志设置为true。

这是我的代码......

$scope.login = function (user) { User.login(user).$promise.then( function (data) { $rootScope.activeUser = data; $rootScope.user_id = $rootScope.activeUser.user.username; console.log('Active User: ', $rootScope.activeUser.user.email); console.log('Status: ', $rootScope.activeUser.user.status); if ($rootScope.activeUser.user.status === 'Y') { $scope.loginError = false; function checkAdmin(eid) { Se_user.findById({ id: eid }).$promise.then( function (data1) { var user_properties = data1; if (user_properties.role === 'Admin') { $rootScope.isAdmin = true; console.log('isAdminInside: ', $rootScope.isAdmin); return true; } else { //$rootScope.isAdmin = false; return false; } }); }; var isAdmin = checkAdmin($rootScope.user_id); console.log('isAdminOutside: ', $rootScope.isAdmin); $state.go('home'); } else { $scope.loginError = true; $scope.loginErrorMessage = "Your account has been disabled. Please contact BMT Support for assistance"; } }, function (err) { console.log('Error: ', err) $scope.loginError = true; $scope.loginErrorMessage = "You've entered an invalid User ID or Password. Please try again."; }); };

我一直在通过写入控制台进行故障排除,这是一个输出示例...

Active User: user@email.com Status: Y isAdminOutside: undefined isAdminInside: true

我应该如何重组,以便在成功登录活动用户后正确返回checkAdmin的结果?

I'm having quite a hard time understanding how to chain promises. I'm writing login function for my app, leverating Loopback's Angular SDK. The objective, upon validating a user's credentials, is to confirm that the user's account is active, then fetch some additional properties including the user's role and set a flag to true if the user has admin privileges.

Here's my code...

$scope.login = function (user) { User.login(user).$promise.then( function (data) { $rootScope.activeUser = data; $rootScope.user_id = $rootScope.activeUser.user.username; console.log('Active User: ', $rootScope.activeUser.user.email); console.log('Status: ', $rootScope.activeUser.user.status); if ($rootScope.activeUser.user.status === 'Y') { $scope.loginError = false; function checkAdmin(eid) { Se_user.findById({ id: eid }).$promise.then( function (data1) { var user_properties = data1; if (user_properties.role === 'Admin') { $rootScope.isAdmin = true; console.log('isAdminInside: ', $rootScope.isAdmin); return true; } else { //$rootScope.isAdmin = false; return false; } }); }; var isAdmin = checkAdmin($rootScope.user_id); console.log('isAdminOutside: ', $rootScope.isAdmin); $state.go('home'); } else { $scope.loginError = true; $scope.loginErrorMessage = "Your account has been disabled. Please contact BMT Support for assistance"; } }, function (err) { console.log('Error: ', err) $scope.loginError = true; $scope.loginErrorMessage = "You've entered an invalid User ID or Password. Please try again."; }); };

I've been troubleshooting by writing to the console, here's a sample of the output...

Active User: user@email.com Status: Y isAdminOutside: undefined isAdminInside: true

How should I restructure so that the result of checkAdmin is properly returned after a successful login of an active user?

最满意答案

尝试更改这部分代码:

function checkAdmin(eid) {
  return Se_user.findById({
    id: eid
  }).$promise.then(
    function(data1) {
      var user_properties = data1;
      if (user_properties.role === 'Admin') {
        $rootScope.isAdmin = true;
        console.log('isAdminInside: ', $rootScope.isAdmin);
        return true;
      } else {
        //$rootScope.isAdmin = false;
        return false;
      }
    });
};

var isAdmin = checkAdmin($rootScope.user_id)
  .then(function(val) {
    console.log('isAdminOutside: ', val);
    $state.go('home');
  });

Try changing this part of code :

function checkAdmin(eid) {
  return Se_user.findById({
    id: eid
  }).$promise.then(
    function(data1) {
      var user_properties = data1;
      if (user_properties.role === 'Admin') {
        $rootScope.isAdmin = true;
        console.log('isAdminInside: ', $rootScope.isAdmin);
        return true;
      } else {
        //$rootScope.isAdmin = false;
        return false;
      }
    });
};

var isAdmin = checkAdmin($rootScope.user_id)
  .then(function(val) {
    console.log('isAdminOutside: ', val);
    $state.go('home');
  });

                    
                     
          

更多推荐

本文发布于:2023-04-29 10:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335972.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:链接   Javascript   chaining   promise

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!