aurelia中“未处理的承诺拒绝:TypeError”的可能原因是什么?

我一直从事Aurelia开发,但休息了两个月。现在,我开始了一个新项目,尝试使用http-fetch-client时遇到未知问题。

控制台显示:

aurelia-fetch-client.js?a909:199 OPTIONS http://url.com/api/login net::ERR_CONNECTION_REFUSED

index.js?07f7:248 Possible Unhandled Promise Rejection: TypeError: Failed to fetch

“网络”标签显示:

Request URL: http://url.com/api/login
Referrer Policy: no-referrer-when-downgrade

令我惊讶的是,我以前的项目(两个月前成功执行了获取客户端请求)现在也显示此错误。 谁能对此问题提供见解? 我为什么要遇到这个?

qzc105 回答:aurelia中“未处理的承诺拒绝:TypeError”的可能原因是什么?

听起来您正在执行异步/等待,并且还没有将await呼叫包装在try/catch中。

某些版本的Aurelia使用Bluebird作为Promise垫片,您可以使用其global rejection events处理这些问题。

这就是看起来的样子。

// NOTE: event name is all lower case as per DOM convention
window.addEventListener("unhandledrejection",function(e) {
    // NOTE: e.preventDefault() must be manually called to prevent the default
    // action which is currently to log the stack trace to console.warn
    e.preventDefault();
    // NOTE: parameters are properties of the event detail property
    var reason = e.detail.reason;
    var promise = e.detail.promise;
    // See Promise.onPossiblyUnhandledRejection for parameter documentation
});

// NOTE: event name is all lower case as per DOM convention
window.addEventListener("rejectionhandled",function(e) {
    // NOTE: e.preventDefault() must be manually called prevent the default
    // action which is currently unset (but might be set to something in the future)
    e.preventDefault();
    // NOTE: parameters are properties of the event detail property
    var promise = e.detail.promise;
    // See Promise.onUnhandledRejectionHandled for parameter documentation
});

当然,您可能正在使用其他Promise垫片。但是,最终try/catch是您的答案。

本文链接:https://www.f2er.com/2978720.html

大家都在问