aurelia-fetch-client -一个承诺被拒绝,没有错误:[object Response]



我正在使用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;
    }
}

感谢您的帮助

aurelia-fetch-client标准配置(通过.useStandardConfiguration()应用于您的代码中)拒绝非成功HTTP响应状态码。在aurelia/fetch-client repo中有一个最近的(关闭的)问题。取回客户端拒绝响应本身的承诺,因此浏览器抱怨(它希望承诺只被错误实例拒绝)。

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

相关内容

最新更新