如何 return Firebase 在 Angular 中被拒绝的承诺
How to return Firebase's rejected promise in Angular
我正在尝试 return 来自 Angular 中 Firebase promise 的 Rejected 函数的一些数据用于 $routeChangeError。出于某种原因,console.error(data) 有效,但下一行 return 数据无效。
这是我的代码:
angular.module('campaignsHub.dashboard', ['ngRoute'])
.run(["$rootScope", "$location", "$route",
function($rootScope, $location, $route) {
$rootScope.$on("$routeChangeError", function(event, next, previous, error) {
// This works if I use return authService.auth.$requireAuth(); and nothing else
if (error === "AUTH_REQUIRED") {
$location.path("/login");
}
});
}
])
.config(['$routeProvider',
function($routeProvider) {
$routeProvider.when('/dashboard', {
templateUrl: 'dashboard/dashboard.html',
controller: 'DashboardCtrl',
resolve: {
"currentAuth": ["authService",
function(authService) {
authService.auth.$requireAuth().then(function(data) {
// RESOLVED
authService.GetCurrentUser().then(function(userData) {
return userData; // This works if user is logged in
})
}, function(data) {
// REJECTED
console.error(data); // This part works - returns AUTH_REQUIRED if user is not logged in
return data; // This part doesn't as it should be picked up by the .run() method
});
}
]
}
});
}
])
要使 promise 正常工作,您必须 return 来自函数的 promise。一旦 promise 得到解决,calle 将获得可用于进一步处理的已解决数据。
所以你肯定需要 return
声明你的 authService inside resolve。
改变这个
function(authService) {
authService.auth.$requireAuth().then(function(data) {
// RESOLVED
authService.GetCurrentUser().then(function(userData) {
return userData; // This works if user is logged in
})
}, function(data) {
// REJECTED
console.error(data); // This part works - returns AUTH_REQUIRED if user is not logged in
return data; // This part doesn't as it should be picked up by the .run() method
});
}
至
function(authService) {
return authService.auth.$requireAuth().then(function(data) {
// RESOLVED
authService.GetCurrentUser().then(function(userData) {
return userData; // This works if user is logged in
})
}, function(data) {
// REJECTED
console.error(data); // This part works - returns AUTH_REQUIRED if user is not logged in
return data; // This part doesn't as it should be picked up by the .run() method
});
}
我已经设法找到了解决方案。为了让它工作,我扩展了 Shankar 的解决方案并在我的 resolve 方法中返回了每一个承诺。所以现在,它看起来像这样:
resolve: {
"currentAuth": ["authService",
function(authService) {
return authService.auth.$requireAuth().then(function(data) {
// RESOLVED
return authService.GetCurrentUser().then(function(userData) {
return userData;
})
}, function(data) {
// REJECTED
throw data;
});
}
]
}
我正在尝试 return 来自 Angular 中 Firebase promise 的 Rejected 函数的一些数据用于 $routeChangeError。出于某种原因,console.error(data) 有效,但下一行 return 数据无效。
这是我的代码:
angular.module('campaignsHub.dashboard', ['ngRoute'])
.run(["$rootScope", "$location", "$route",
function($rootScope, $location, $route) {
$rootScope.$on("$routeChangeError", function(event, next, previous, error) {
// This works if I use return authService.auth.$requireAuth(); and nothing else
if (error === "AUTH_REQUIRED") {
$location.path("/login");
}
});
}
])
.config(['$routeProvider',
function($routeProvider) {
$routeProvider.when('/dashboard', {
templateUrl: 'dashboard/dashboard.html',
controller: 'DashboardCtrl',
resolve: {
"currentAuth": ["authService",
function(authService) {
authService.auth.$requireAuth().then(function(data) {
// RESOLVED
authService.GetCurrentUser().then(function(userData) {
return userData; // This works if user is logged in
})
}, function(data) {
// REJECTED
console.error(data); // This part works - returns AUTH_REQUIRED if user is not logged in
return data; // This part doesn't as it should be picked up by the .run() method
});
}
]
}
});
}
])
要使 promise 正常工作,您必须 return 来自函数的 promise。一旦 promise 得到解决,calle 将获得可用于进一步处理的已解决数据。
所以你肯定需要 return
声明你的 authService inside resolve。
改变这个
function(authService) {
authService.auth.$requireAuth().then(function(data) {
// RESOLVED
authService.GetCurrentUser().then(function(userData) {
return userData; // This works if user is logged in
})
}, function(data) {
// REJECTED
console.error(data); // This part works - returns AUTH_REQUIRED if user is not logged in
return data; // This part doesn't as it should be picked up by the .run() method
});
}
至
function(authService) {
return authService.auth.$requireAuth().then(function(data) {
// RESOLVED
authService.GetCurrentUser().then(function(userData) {
return userData; // This works if user is logged in
})
}, function(data) {
// REJECTED
console.error(data); // This part works - returns AUTH_REQUIRED if user is not logged in
return data; // This part doesn't as it should be picked up by the .run() method
});
}
我已经设法找到了解决方案。为了让它工作,我扩展了 Shankar 的解决方案并在我的 resolve 方法中返回了每一个承诺。所以现在,它看起来像这样:
resolve: {
"currentAuth": ["authService",
function(authService) {
return authService.auth.$requireAuth().then(function(data) {
// RESOLVED
return authService.GetCurrentUser().then(function(userData) {
return userData;
})
}, function(data) {
// REJECTED
throw data;
});
}
]
}