【问题标题】:AngularJS 1.7.9 : how to debug "Possibly unhandled rejection: {}"?AngularJS 1.7.9:如何调试“可能未处理的拒绝:{}”?
【发布时间】:2020-02-22 09:37:09
【问题描述】:

我知道类似的问题,例如 Angularjs 1.7.9 - Possibly unhandled rejection 以及其中提到的重复问题。

但是,我的代码不使用承诺(据我所知;当然没有 $promise$http)。

我只是为朋友敲了一个简单的ui-router 演示。它只是两个视图,每个视图都有一个切换到另一个视图的按钮。它适用于 AngulrJs 1.5,并在 1.7 中打破上述错误。

虽然很简单,但要发布的代码太多了。以防万一,我不想在我的代码中找到错误,而是希望有一个规范的答案来帮助将来阅读此问题的其他人:如何消除此错误消息?

错误:转换失败(由“可能未处理的拒绝:{}”引起)

at r [as $get] (http://localhost/common/js/third_party/ui-router_zero_pint_2_point_11/angular-ui-router.min.js:7:11365)  
at Object.invoke (http://localhost/common/js/third_party/angular-1_point_7_point_9/angular.min.js:45:62)  
at http://localhost/common/js/third_party/angular-1_point_7_point_9/angular.min.js:46:365  
at d (http://localhost/common/js/third_party/angular-1_point_7_point_9/angular.min.js:43:495)  
at e (http://localhost/common/js/third_party/angular-1_point_7_point_9/angular.min.js:44:235)  
at Object.invoke (http://localhost/common/js/third_party/angular-1_point_7_point_9/angular.min.js:44:320)  
at http://localhost/common/js/third_party/angular-1_point_7_point_9/angular.min.js:47:18  
at r (http://localhost/common/js/third_party/angular-1_point_7_point_9/angular.min.js:8:76)  
at fb (http://localhost/common/js/third_party/angular-1_point_7_point_9/angular.min.js:46:499)  
at c (http://localhost/common/js/third_party/angular-1_point_7_point_9/angular.min.js:22:57)  

【问题讨论】:

  • 谢谢。关于不使用缩小的 AngaulrJs 的重要提示。我不知道“解析器”是什么,但谷歌会。再次感谢(点赞)
  • 我会回复@wu-lee 对this question的回答
  • 如果您禁用$qProvider 中的“未处理拒绝”错误,代码是否有效?
  • 它确实有效,只是给出了那些恼人的错误消息,出于明显的原因,我不希望全面压制。
  • 如果您使用angular.js 而不是angular.min.jsangular-ui-router.js 而不是angular-ui-router.min.js,您将获得更多信息的堆栈跟踪。 Angular-UI-Router 在它的转换模块中使用了 Promise。看起来您升级了 AngularJS 的版本而没有升级 Angular-UI-Router 的版本。将您的路由器从 V0.2.11 升级到 V0.4.3。看起来您的问题是由草率的 Angular-UI-Router 代码引起的。如果他们没有通过 V0.4.3 解决问题,您可以修补库或使用消息。

标签: angularjs debugging error-handling angular-ui-router angular-promise


【解决方案1】:

最新版本的 UI-router (1.0.25) 解决了这个问题。随时发布答案。

如果您使用angular.js 而不是angular.min.jsangular-ui-router.js 而不是angular-ui-router.min.js,您将获得更多信息的堆栈跟踪。 Angular-UI-Router 在其转换模块中使用了 Promise。看起来您升级了 AngularJS 的版本而没有升级 Angular-UI-Router 的版本。将您的路由器从 V0.2.11 升级到 V0.4.3。看起来您的问题是由草率的 Angular-UI-Router 代码引起的。如果他们没有通过 V0.4.3 解决问题,您可以修补库或使用消息。

调试"possibly unhandled rejection"

堆栈跟踪将显示错误产生的文件和行号。检查代码并解决问题。如果错误源于第三方库,请尝试升级到最新版本或联系第三方库供应商。

作为最后的手段,禁用"possibly unhandled rejection" 消息:

app.config(functon ($qProvider) {
    $qProvider.errorOnUnhandledRejections(false);
}); 

不推荐这样做,因为它允许其他问题静默失败。

如果不关心特定承诺失败,请添加.catch 处理程序:

$http(config).then(function(response) {
    //...
}).catch(function(response) {
   //I don't care about errors
   if (debug) console.log(response);
   //Re-throw error response
   throw response;
})

升级 AngularJS

升级 AngularJS 时,最好同时升级所有 AngularJS 模块。那是从angular.js@1.4迁移到angular.js@1.5,同时升级到angular-animate@1.5angular-resource@1.5angular-route.js@1.5等。我在尝试混合和匹配AngularJS模块的版本时遇到了令人不快的问题。

迁移时,我建议一次升级一个次要版本。例如,先从 V1.4 升级到 V1.5,修复故障,然后升级到 V1.6。

当前版本是1.7.9 pollution-eradication (2019-11-19)。我建议使用最新版本,因为 AngularJS 团队已承诺仅修复 V1.2.x 和最新版本中的安全漏洞。 有关详细信息,请参阅

升级 Angular-UI-Router

AngularJS 的 UI-Router 有两个主要版本

版本0.4.3UI-Router-Legacy

版本1.0.25UI-Router for AngularJS

我建议在迁移到最新版本的 UI-Router for AngularJS 之前升级到最新版本的 UI-Router-Legacy。两者之间发生了重大的重大变化,最好逐步处理。

有关更多信息,请参阅

【讨论】:

    【解决方案2】:

    调试ui-router 的一种方法如下:

    从控制台注入$state 服务,输入以下内容:

    var test = angular.element(document.body).injector().get('$state');
    

    然后模拟并执行导致问题的过渡:

    test.go('root.details') // use the state url here
    

    之后,转换的详细信息将打印在控制台中。进入$$state对象,你可以找到更多关于失败转换和失败原因的细节:

    【讨论】:

    • 它给了我“`Uncaught Error: Could not resolve 'root.details' from state 'search_result'” :-(
    • 好的,您似乎已经声明了一个名为search_result 的状态,然后您尝试转到状态root.details。这是一个无效的转换并且失败。将状态 search_result 更改为以 root. 开头的状态,例如:root.results 并重试..
    • 我什至没有一个名为 details 的州!另外,我不注入$rootscope,因为我认为这是不好的做法。那么,我该如何尝试更改为 root.details
    • 我没有很好地解释我的错误。如果在将状态从一个视图更改为另一个视图时发生错误(例如,如果您的状态是state1state2),则转到第一个视图state1,然后从控制台执行上述脚本test.go('state2')
    • 错误消息说:Error: transition failed..,所以我假设在转换期间发生错误。也许下面正在发生转变
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-19
    • 2016-11-24
    • 2017-11-02
    • 2017-10-08
    • 2017-05-07
    • 2018-02-27
    • 1970-01-01
    相关资源
    最近更新 更多