Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comments: Add a Get Note API #24

Closed
jayair opened this issue Apr 10, 2017 · 29 comments

Comments

@jayair
Copy link
Member

commented Apr 10, 2017

@jayair jayair added the Discussion label Apr 10, 2017

@willybeans

This comment has been minimized.

Copy link

commented May 12, 2017

Getting this when running the mock noteId parameter (serverless webpack invoke --function get --path event.json)

Type Error ---------------------------------------------

 Cannot read property 'handler' of undefined
@jayair

This comment has been minimized.

Copy link
Member Author

commented May 12, 2017

@willybeans can you check your serverless.yml and look for the handler property?

@willybeans

This comment has been minimized.

Copy link

commented May 12, 2017

@jayair here is the bottom part of the file:

  get:
    # Defines an HTTP API endpoint that calls the main function in get.js
    # - path: url path is /notes/{id}
    # - method: GET request
    handler: get.main
    events:
      - http:
          path: notes/{id}
          method: get
          cors: true
          authorizer:
            arn: arn:aws:cognito-idp:us-east-2:433151866650:userpool/us-east-2_mttWckAZq 
@fwang

This comment has been minimized.

Copy link
Member

commented May 12, 2017

@willybeans this error means the get handler was not found in serverless.yml. Could you share the rest of your serverless.yml file?

@willybeans

This comment has been minimized.

Copy link

commented May 13, 2017

@fwang

service: notes-app-api

plugins:
  - serverless-webpack

custom:
  webpackIncludeModules: true

provider:
  name: aws
  runtime: nodejs6.10
  stage: prod
  region: us-east-2

  iamRoleStatements:
    - Effect: Allow
      Action:
        - dynamodb:DescribeTable
        - dynamodb:Query
        - dynamodb:Scan
        - dynamodb:GetItem
        - dynamodb:PutItem
        - dynamodb:UpdateItem
        - dynamodb:DeleteItem
      Resource: "arn:aws:dynamodb:us-east-1:*:*"

  get:
    # Defines an HTTP API endpoint that calls the main function in get.js
    # - path: url path is /notes/{id}
    # - method: GET request
    handler: get.main
    events:
      - http:
          path: notes/{id}
          method: get
          cors: true
          authorizer:
            arn: arn:aws:cognito-idp:us-east-2:433151866650:userpool/us-east-2_mttWckAZq
@jayair

This comment has been minimized.

Copy link
Member Author

commented May 13, 2017

@willybeans just noticed that you are missing the functions block. Have a look at the serverless.yml from this chapter - https://github.com/AnomalyInnovations/serverless-stack-demo-api/blob/add-a-get-note-api/serverless.yml#L29.

Notice, the get block is inside the functions block.

@Jaikant

This comment has been minimized.

Copy link

commented May 21, 2017

I had to mark the main function as 'async' to avoid the error
SyntaxError: await is a reserved word

@jayair

This comment has been minimized.

Copy link
Member Author

commented May 21, 2017

@Jaikant

This comment has been minimized.

Copy link

commented May 22, 2017

I seem to have missed that, maybe while moving through the chapters. thanks :)

@idowolf

This comment has been minimized.

Copy link

commented Jun 12, 2017

When I run the command

serverless webpack invoke --function get --path mocks/get-event.json
I'm getting

Serverless: Bundling with Webpack...
Time: 617ms
     Asset     Size  Chunks             Chunk Names
 create.js   5.8 kB       0  [emitted]  create
    get.js  5.88 kB       1  [emitted]  get
handler.js  2.28 kB       2  [emitted]  handler
Serverless: Run function get...
{ statusCode: 500,
  headers: 
   { 'Access-Control-Allow-Origin': '*',
     'Access-Control-Allow-Credentials': true },
  body: '{"status":false,"error":"Item not found."}' }

But when I ran the "create" command before it went well with statusCode 200.
What could have gone wrong?

@jayair

This comment has been minimized.

Copy link
Member Author

commented Jun 12, 2017

@idowolf The get function uses the id from the note you created in the Create chapter. So make sure to replace this https://github.com/AnomalyInnovations/serverless-stack-demo-api/blob/add-a-get-note-api/mocks/get-event.json#L3 with the one you get when you run the create command.

@yashg5

This comment has been minimized.

Copy link

commented Jul 28, 2017

How come item not found will be status code 500. i think there has to be another way to respond with proper status codes like 400, 404

@jayair

This comment has been minimized.

Copy link
Member Author

commented Jul 28, 2017

@yashg5 Yeah we leave that to you. It's pretty straightforward, the status codes are being set here - https://github.com/AnomalyInnovations/serverless-stack-demo-api/blob/master/libs/response-lib.js#L6

@sdooley-ebsco

This comment has been minimized.

Copy link

commented Nov 9, 2017

When running...

serverless invoke local --function get --path mocks/get-event.json

I get the following type error "lambda is not a function". Any thoughts on what is causing this problem?

@jayair

This comment has been minimized.

Copy link
Member Author

commented Nov 9, 2017

@sdooley-ebsco It might be because get is probably not pointing to a function? It's most likely a typo in your serverless.yml.

@omairhamid

This comment has been minimized.

Copy link

commented Dec 31, 2017

In "get.js", these are the first two lines:
import * as dynamoDbLib from "./libs/dynamodb-lib";
import { success, failure } from "./libs/response-lib";

I thought I had installed my AWS CLI and other tools correctly, but when I run my /mocks/get-event.json, I get the following error:

ERROR in ./get.js
Module not found: Error: Can't resolve './libs/dynamodb-lib' in '/notes-app-api'
@ ./get.js 68:19-49

ERROR in ./get.js
Module not found: Error: Can't resolve './libs/response-lib' in '/notes-app-api'
@ ./get.js 72:19-49

Where should it be finding /libs/response-lib and /libs/dynamodb-lib on my macOS, so that I can update the paths in get.js accordingly?

Thank you

@jayair

This comment has been minimized.

Copy link
Member Author

commented Dec 31, 2017

@omairhamid

This comment has been minimized.

Copy link

commented Dec 31, 2017

@jayair Thank you. I have no idea how I missed that. Happy new year!

@starkos

This comment has been minimized.

Copy link

commented Jan 25, 2018

When running…

serverless invoke local --function get --path mocks/get-event.json

I get the error…

 Unexpected token u in JSON at position 0

Which appears related to this SO question. Adding…

"body": "{\"text\":\"\"}"

…to get-event.json fixed it.

@jayair

This comment has been minimized.

Copy link
Member Author

commented Jan 27, 2018

@starkos That shouldn't be the case for a GET request. Can you check your serverless.yml and ensure that method: get is set for this function?

@starkos

This comment has been minimized.

Copy link

commented Jan 29, 2018

Confirmed:

  get:
    handler: get.main
    events:
      - http:
          path: notes/{id}
          method: get
          cors: true
          authorizer: aws_iam
@jayair

This comment has been minimized.

Copy link
Member Author

commented Jan 29, 2018

@starkos I see. This is weird. Which version of Serverless are you using?

@starkos

This comment has been minimized.

Copy link

commented Jan 29, 2018

1.25.0

@jayair

This comment has been minimized.

Copy link
Member Author

commented Jan 31, 2018

@starkos Can you try it with the repo in the tutorial and see if you get the same thing? https://github.com/AnomalyInnovations/serverless-stack-demo-api/tree/add-a-get-note-api

@karthickng

This comment has been minimized.

Copy link

commented Mar 19, 2018

I faced a ResourceNotFoundException for a while trying to execute the code for this chapter.
Turned out, I needed to configure the AWS region again in get.js, despite issuing a create call earlier that was already doing this.

Can someone explain why this may be needed?

@jayair

This comment has been minimized.

Copy link
Member Author

commented Mar 19, 2018

@karthickng Do you have multiple AWS credentials?

@smittym

This comment has been minimized.

Copy link

commented Apr 1, 2018

I had to do the same thing as karthickng and I do not have multiple AWS credentials.

@jayair

This comment has been minimized.

Copy link
Member Author

commented Apr 2, 2018

@smittym @karthickng We moved the AWS.config.update({ region: "us-east-1" }); line inside src/libs/dynamodb-lib.js in the previous chapter. So just make sure it is in there for this chapter as well.

@jayair jayair closed this May 9, 2018

@jayair jayair reopened this May 9, 2018

@jayair

This comment has been minimized.

Copy link
Member Author

commented May 9, 2018

@jayair jayair closed this May 9, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
You can’t perform that action at this time.