Call the Create API

This sounds like the CORS headers are not being returned in your Lambda functions?

We’ve moved the steps around but make sure that you have this in your Lambda functions.

Hi @jayair! Thanks for the reply. Yes I think I have those covered. My my files are here if you have time: https://github.com/jeffcordova/serverlesss-stack-api. Do you think it’s a backend issue even if the test in the previous chapters were successful? Or how do I debug this? Thank you!

Hi @jayair, I tried logging my response in the Lambda function. It seems it’s returning the correct headers.

2020-11-16T03:59:07.661Z f69bcc1f-2f21-4793-a37c-f09011fcd66d INFO { statusCode: 200, body: '{"userId":"ap-southeast-1:XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX","noteId":"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX","content":"test today","createdAt":1605499147577}', headers: { 'Access-Content-Allow-Origin': '*', 'Access-Content-Allow-Credentials': true }}

So I’m guessing the issue is with my API Gateway? If you have suggestions on how to knock this one down, feel free to suggest. Thank you.

Yeah API Gateway needs to handle the OPTIONS request as well. For that you need this:

Hi Jeff, don’t know whether you solved this issue. I looked at the create function and found that my function wasn’t using the handler-lib which we edit in the CORS Handle CORS in Serverless APIs | Serverless Stack.
I redeployed the backend with the create function using this handler and it returned a 200. :slight_smile:

1 Like

:pray: :pray: :pray:

I was stuck with a network issue for many, many hours. It was basically a lambda timeout error (error code “500”) after submitting the note. I tried to troubleshoot it without success, and I also followed the entire guide from start to end. However, I couldn’t identify what I did wrong. Then, without any other options, I decided to redeploy, and the problem was fixed. Even though it’s a basic solution, I thought it would be better to leave a comment here in case someone gets stuck with the same problem - try redeploying first.

1 Like

I got this, too. The lambda was failing after about 10 seconds. So I upped it to 30 seconds which seemed to fix it for me in dev. I don’t know if this is needed in production. There should be a way to add an environment variable to optionally add this timeout only in dev.
P.S. In the ApiStack.ts add timeout:30 to the defaults.function next to the bind property like this:

const api = new Api(stack, "Api", {
        defaults: {
            authorizer: "iam",
            function: {
                timeout: 30,
                bind: [table, STRIPE_SECRET_KEY]
            }
        },

There was another error because somehow I had created a note without a content. This caused the trim function to error. I deleted the offending entry. I typically prevent this error either by adding a default parameter (i.e. content = “” (an empty string)) in the note destructuring. But this doesn’t fix it in all cases so I have also added a default to the trim function ((content || “”).trim()…) which usually fixes it.