0

Im brand new to AWS API Gateways and Lambda. I am trying to get the basic 'Hello from Lambda!' function to work.

I can sign in successfully as below:

const user = await Auth.signIn(email, password);

I get A token back which is correct as I tested it on the AWS API console and got the response

//token works as tested on AWS Console (API Tester)
        const token = user2.signInUserSession.idToken.jwtToken

I assume that the request info is correct as the token is correct:

  const requestInfo = {
    headers: {
      Authorization: token
    }
   }

I think the error is here as whenever I try to do anything with the 'Data' I don't get a response

 const data = await API.get('API_NAME','/hello',requestInfo)
 console.log('This is the Data: ',{ data } )

The Error I get in the try/catch Xcode is:

'API API_NAME does not exist'

any suggestions?

Error I get when I run Amplify Push:

Packaging lambda failed function failed with the error 
Command failed with exit code 1: npm install
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: APP_NAME@1.0.1
npm ERR! Found: react@16.8.6
npm ERR! node_modules/react
npm ERR!   react@"16.8.6" from the root project
npm ERR!   peer react@"^16.7.0" from @aws-amplify/ui-react@0.2.36
npm ERR!   node_modules/@aws-amplify/ui-react
npm ERR!     @aws-amplify/ui-react@"^0.2.35" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^16.14.0" from react-dom@16.14.0
npm ERR! node_modules/react-dom
npm ERR!   peer react-dom@"^16.7.0" from @aws-amplify/ui-react@0.2.36
npm ERR!   node_modules/@aws-amplify/ui-react
npm ERR!     @aws-amplify/ui-react@"^0.2.35" from the root project
npm ERR! 
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
npm ERR! 
npm ERR! See /Users/name/.npm/eresolve-report.txt for a full report.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/name/.npm/_logs/2021-02-02T09_36_01_091Z-debug.log
An error occurred during the push operation: Packaging lambda failed function failed with the error 
Command failed with exit code 1: npm install
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: APP_NAME@1.0.1
npm ERR! Found: react@16.8.6
npm ERR! node_modules/react
npm ERR!   react@"16.8.6" from the root project
npm ERR!   peer react@"^16.7.0" from @aws-amplify/ui-react@0.2.36
npm ERR!   node_modules/@aws-amplify/ui-react
npm ERR!     @aws-amplify/ui-react@"^0.2.35" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^16.14.0" from react-dom@16.14.0
npm ERR! node_modules/react-dom
npm ERR!   peer react-dom@"^16.7.0" from @aws-amplify/ui-react@0.2.36
npm ERR!   node_modules/@aws-amplify/ui-react
npm ERR!     @aws-amplify/ui-react@"^0.2.35" from the root project
npm ERR! 
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
npm ERR! 
npm ERR! See /Users/name/.npm/eresolve-report.txt for a full report.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/name/.npm/_logs/2021-02-02T09_36_01_091Z-debug.log
mick1996
  • 516
  • 9
  • 31

1 Answers1

1

As you can sign in successfully I suppose you've configured Amplify something like this specifying AWS poolId, clientId etc.

Similarly you've to do for APIs also like documented here

This is the format, suppose API name is account.

Amplify.configure({
  Auth: { ... },

  API: {
    endpoints: [
      {
        name: 'account', // name of API like 'account', no '/'
        endpoint: BASE_URL, // like 'https://api_name.com', don't put '/' at the end
        region: 'us-east-1', // your region like 'us-east-1', 'ap-southeast-1' etc.
      },
      ... // Other APIs
    ]
  }
});

Then when you call the API this would be the format

const accountRes: AccountData = await API.get('account', '/account', requestInfo);

NOTE:- If API name is same but request type is different (GET, POST, PUT etc.) you don't need to do multiple entries, You can simply call respective method for the request API.get(), API.post(), API.put() etc.

Ashu
  • 501
  • 2
  • 8
  • Hi there! thank you for your help but unfortunately I am doing all of this and it still doesn't work. I tried running amplify push and I get a new error as I have updated my question – mick1996 Feb 02 '21 at 09:39
  • Get this error sometimes in react-native, Don't know exact steps to fix it. try [this](https://stackoverflow.com/questions/64573177/unable-to-resolve-dependency-tree-error-when-installing-npm-packages) or [this](https://stackoverflow.com/a/60482583) or delete **node_modules** then `npm install`. – Ashu Feb 02 '21 at 16:09
  • Had to start the project again. head is wrecked! would you mind having a look at my new question here: https://stackoverflow.com/questions/66014223/aws-cognito-user-able-to-sign-in-but-api-does-not-exist-when-using-i-can-acce – mick1996 Feb 02 '21 at 17:22