AngularJS 1.7.9:如何调试“可能未处理的拒绝:{}"? [英] AngularJS 1.7.9 : how to debug "Possibly unhandled rejection: {}"?

查看:132
本文介绍了AngularJS 1.7.9:如何调试“可能未处理的拒绝:{}"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道一些类似的问题,例如 Angularjs 1.7.9-可能未处理的拒绝和其中重复的内容.

但是,我的代码没有使用promises(我知道;当然不是$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)  

解决方案

最新版本的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);
}); 

不建议这样做,因为它会使其他问题默默地失败.

如果不关心某个特定的Promise失败,则添加一个.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模块的版本时,我遇到了不愉快的问题./p>

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

当前版本为1.7.9 pollution-eradication (2019-11-19).我建议使用最新版本,因为AngularJS团队致力于仅在V1.2.x和最新版本中修复安全错误. 有关更多信息,请参见

升级Angular-UI-路由器

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

版本0.4.3 UI-路由器传统

版本1.0.25 用于AngularJS的UI路由器

我建议先迁移到最新版本的UI-Router-Legacy,然后再迁移到最新版本的UI-Router for AngularJS.两者之间发生了重大的突破性变化,最好逐步解决.

有关更多信息,请参见

I am aware of siiar questions, such as Angularjs 1.7.9 - Possibly unhandled rejection and those mentioned in it as duplicates.

However, my code does not use promises (that I am aware of; certainly no $promise or $http).

I am just knocking up a simple ui-router demo for a friend. It is just two views, each with a button that toggles to the other view. It works just fine with AngulrJs 1.5, and breaks with the above error in 1.7.

A simple as it is, it's a bit too much code to post. In case, rather than finding the error in my code, I woudl like a canonical answer to help others who read this question in future : how to go about denugging this error message?

Error: transition failed (caused by "Possibly unhandled rejection: {}")

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)  

解决方案

The latest version of UI-router (1.0.25) solved the problem. Feel free to post an answer.

You will get a more informative stack trace if you use angular.js instead of angular.min.js and angular-ui-router.js instead of angular-ui-router.min.js. Angular-UI-Router uses promises in its transition module. It looks like you upgraded your version of AngularJS without upgrading the version of Angular-UI-Router. Upgrade your router from V0.2.11 to V0.4.3. It looks like your problem is caused by sloppy Angular-UI-Router code. If they didn't fix the issue by V0.4.3, you can either patch the library or live with the messages.

Debugging "possibly unhandled rejection"

The stack trace will show the file and the line number from which the error originates. Examine the code and fix the problem. If the error orginates in a third- party library, try upgrading to the latest version or contacting the third-party library vendor.

As a last resort, disable the "possibly unhandled rejection" messages:

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

This is not recommended as it allows other problems to silently fail.

If one doesn't care about a specific promise failing, add a .catch handler:

$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;
})

Upgrading AngularJS

When upgrading AngularJS, it is best to upgrade all of the AngularJS modules at the same time. That is when migrating from angular.js@1.4 to angular.js@1.5, at the same time upgrade to angular-animate@1.5, angular-resource@1.5, angular-route.js@1.5, etc. I have seen unpleasant problems when trying to mix and match versions of AngularJS modules.

When migrating, I recommend upgrading one minor version at a time. For example, upgrade from V1.4 to V1.5 first, fix what breaks, then upgrade to V1.6.

The current version is 1.7.9 pollution-eradication (2019-11-19). I recommend using the latest version as the AngularJS team has committed to fixing security bugs only in both V1.2.x and the latest version. For more information, see

Upgrading Angular-UI-Router

UI-Router for AngularJS has two major versions

Version 0.4.3 UI-Router-Legacy

Version 1.0.25 UI-Router for AngularJS

I recommend upgrading to the latest version of UI-Router-Legacy before migrating to the latest version of UI-Router for AngularJS. There have been major breaking changes between the two and it is best to deal with it incrementally.

For more informaton, see

这篇关于AngularJS 1.7.9:如何调试“可能未处理的拒绝:{}"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆