【问题标题】:aurelia-fetch-client - a promise was rejected with a non-error: [object Response]aurelia-fetch-client - 一个承诺被一个非错误拒绝:[对象响应]
【发布时间】:2017-01-20 08:51:13
【问题描述】:

我正在使用 aurelia-fetch-client,当我向我的 nodejs 后端 api 发送请求时收到此错误:

Warning: a promise was rejected with a non-error: [object Response]
    at http://localhost:9000/scripts/vendor-bundle.js:39700:20
    at Array.reduce (native)
    at applyInterceptors (http://localhost:9000/scripts/vendor-bundle.js:39696:33)
    at processResponse (http://localhost:9000/scripts/vendor-bundle.js:39688:12)
    at http://localhost:9000/scripts/vendor-bundle.js:39603:18
From previous event:
    at http://localhost:9000/scripts/vendor-bundle.js:39602:24
From previous event:
    at HttpClient.<anonymous> (http://localhost:9000/scripts/vendor-bundle.js:39590:64)
    at HttpClient.fetch (http://localhost:9000/scripts/vendor-bundle.js:39574:23)
    at AuthService.login (http://localhost:9000/scripts/app-bundle.js:126:30)
    at Login.login (http://localhost:9000/scripts/app-bundle.js:190:30)
    at CallScope.evaluate (http://localhost:9000/scripts/vendor-bundle.js:24067:21)
    at Listener.callSource (http://localhost:9000/scripts/vendor-bundle.js:27508:42)
    at http://localhost:9000/scripts/vendor-bundle.js:27532:24
    at HTMLDocument.handleDelegatedEvent (http://localhost:9000/scripts/vendor-bundle.js:25721:11)

一切正常,但这个警告很烦人,我不知道如何解决它,这是发送请求的代码:

import {HttpClient, json} from 'aurelia-fetch-client';
import baseConfig from 'config';

export class AuthService {
    constructor() {
        this.http = new HttpClient().configure(config => {
            config
                .withBaseUrl(baseConfig.baseUrl)
                .useStandardConfiguration();
        });

        this.isAuthenticated = false;
    }

    login(credentials) {
        return this.http.fetch('/login', {
            method: 'post',
            body: json(credentials)
        })
            .then(res => {
                this.saveToken(res.token)
                return Promise.resolve();
            });
    }

    saveToken(token) {
        localStorage.setItem('token', token);
        this.isAuthenticated = true;
    }
}

任何帮助表示赞赏

【问题讨论】:

  • 这只是一个警告。如果您觉得它没有帮助,请忽略它。
  • 拒绝似乎来自(您的?)拦截器之一。请向我们展示该代码。
  • 我没有写任何拦截器,所以不能来自我的一个

标签: node.js promise aurelia aurelia-fetch-client


【解决方案1】:

aurelia-fetch-client 标准配置(通过 .useStandardConfiguration() 在您的代码中应用)拒绝不成功的 HTTP 响应状态代码。在 aurelia/fetch-client repo here 中有一个最近(已关闭)的问题。 fetch 客户端拒绝了带有响应本身的承诺,因此浏览器正在抱怨(它希望仅在出现错误的情况下拒绝承诺)。

我在自己的代码中通过删除.useStandardConfiguration() 解决了这个问题,因为我在这个特定项目中不需要它。您可以查看the source code 以了解更多有关配置的信息。

【讨论】:

    猜你喜欢
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 2019-04-26
    • 1970-01-01
    相关资源
    最近更新 更多