0

我有一个 angular 6 和 aws-amplify/aws-amplify-angular 应用程序我正在开发,目前我正在尝试做一个 HTTP GET。我收到了Error: Network Error一条我读过的通用消息,实际上可能是从身份验证到与亚马逊的沟通问题的任何内容。

我的函数看起来像这样,我试图简单地将来自我的 lambda 的响应记录到控制台。

async getOwnerDecks() {
    const authToken = await Auth.currentSession().then(data => {
        return data.idToken.jwtToken;
    });

    const authHeader = {
        Authorization: authToken
    };

    this.result = await this.amplifyService.api().get('decks', '', {headers: authHeader}).then( resp => {
        return resp;
        }).catch(err => {
            console.log(err);
    });
}

就是这样。我可以通过 Postman 调用,通过来自 awsmobile 的这个命令awsmobile cloud-api invoke decks get /decks并获得成功的响应,但从上面的函数中我什么也得不到。我试过使用 API.get 和 angularService.api().get 都失败了。

Error
columnNumber: 15
​
config: Object { timeout: 0, xsrfCookieName: "XSRF-TOKEN", xsrfHeaderName: "X-XSRF-TOKEN", … }
​
fileName: "http://localhost:8080/vendor.js"
​
lineNumber: 96651
​
message: "Network Error"
​
request: XMLHttpRequest { __zone_symbol__xhrSync: false, __zone_symbol__xhrURL: "https://xxxxxx.execute-api.eu-west-1.amazonaws.com/MobileHub_Deployments/decks", __zone_symbol__ON_PROPERTYreadystatechange: handleLoad(), … }
​
response: undefined
​
stack: "createError@http://localhost:8080/vendor.js:96651:15\nhandleError@http://localhost:8080/vendor.js:96194:14\nwrapFn@http://localhost:8080/polyfills.js:3510:30\n./node_modules/zone.js/dist/zone.js/</ZoneDelegate.prototype.invokeTask@http://localhost:8080/polyfills.js:2743:17\nonInvokeTask@http://localhost:8080/vendor.js:40796:24\n./node_modules/zone.js/dist/zone.js/</ZoneDelegate.prototype.invokeTask@http://localhost:8080/polyfills.js:2742:17\n./node_modules/zone.js/dist/zone.js/</Zone.prototype.runTask@http://localhost:8080/polyfills.js:2510:28\n./node_modules/zone.js/dist/zone.js/</ZoneTask.invokeTask@http://localhost:8080/polyfills.js:2818:24\ninvokeTask@http://localhost:8080/polyfills.js:3862:9\nglobalZoneAwareCallback@http://localhost:8080     /polyfills

​</p>

这是我应该得到的回应

{ 
  requestBody: null,
  pathParams: '/decks',
  queryStringParams: null,
  headerParams:
   { Accept: 'application/json, text/plain, */*',
     'CloudFront-Forwarded-Proto': 'https',
     'CloudFront-Is-Desktop-Viewer': 'true',
     'CloudFront-Is-Mobile-Viewer': 'false',
     'CloudFront-Is-SmartTV-Viewer': 'false',
     'CloudFront-Is-Tablet-Viewer': 'false',
     'CloudFront-Viewer-Country': 'ES',
     Host: 'xxxxxxxxxxxxx.execute-api.eu-west-1.amazonaws.com',
     'User-Agent': 'axios/0.17.1',
     Via: '1.1 xxxxxxxxxxxxxxx.cloudfront.net (CloudFront)',
     'X-Amz-Cf-Id': 'iNPucPQXZybc7Zg1AfA005rE-W30Qs-TG1V2pDK5fDHAPWSNSBg==',
     'x-amz-date': '20180620T143059Z',
     'X-Amzn-Trace-Id': 'Root=1-5b2a6523-3c88e3d2f77853e8cbf4351c',
     'X-Forwarded-For': 'xxx.xxx.xxx.xxx,xxx.xxx.xxx.xxx',
     'X-Forwarded-Port': '443',
     'X-Forwarded-Proto': 'https' },
  stage: 'Development',
  stageVariables: { stage: 'Development' },
  cognitoIdentityId: null,
  httpMethod: 'GET',
  sourceIp: 'xxx.xxx.xxx.xxx',
  userAgent: 'axios/0.17.1',
  requestId: '8cec8ff3-7496-11e8-a737-9d1996926e66',
  resourcePath: '/decks' 
}

我正在使用最新版本 6 angular/cli、aws-amplify 和 aws-amplify-angular npm 包。

我已经验证它与 cors 无关,因为我之前遇到了 cors 标头问题,现在一旦我将所需的标头添加到 awsmobile lambda,错误就消失了。

有什么明显的我失踪了吗?

4

1 回答 1

0

您有以下代码:

this.result = await this.amplifyService.api().get('decks', '', {headers: authHeader}).then( resp => {
    return resp;
    }).catch(err => {
        console.log(err);
});

看起来您在 .get 参数中传递了一个空路径。如果你有这样的事情......

this.result = await this.amplifyService.api().get('decks', '/decks', {headers: authHeader}).then( resp => {
    return resp;
    }).catch(err => {
        console.log(err);
});

...在其中传递 /decks 路径而不是空字符串?

于 2018-06-21T19:37:45.980 回答