我很难理解如何将承诺联系起来。 我正在为我的应用程序编写登录功能,对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: trueHow 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'); });
更多推荐
发布评论