简体   繁体   中英

Angular 6 and aws-amplify generic network error

I have an angular 6 and aws-amplify/aws-amplify-angular app I am working on and current I am trying to do a HTTP GET. I am getting a generic Error: Network Error message which I have read can really be anything from auth to communication problems with amazon.

My function looks like this and I am trying to simply log to the console the response from my 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);
    });
}

And that is it. I can invoke via Postman, via this command from awsmobile awsmobile cloud-api invoke decks get /decks and get successful responses but from the function above I get nothing. I have tried using API.get and angularService.api().get and all fail.

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

Here are the response I should get

{ 
  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' 
}

I am using the latest version 6 angular/cli, aws-amplify and aws-amplify-angular npm packages.

I have verified it is not cors related, as I was getting a cors header issue before and now the error has gone away once I added the required headers to the awsmobile lambda.

Anything obvious I am missing?

You have the following code:

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

It looks like you are passing in an empty path in the .get parameters. Should you have something like this...

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

...in which the /decks path is being passed instead of an empty string?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM