question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invoke Error: Cannot Read Property '0' of Undefined

See original GitHub issue

Describe the bug

This is a bug that prevents the NextJS lambda from serving the site. It happens almost every other request and results in timeouts or cloudfront returning a 503.

Actual behavior

Causes timeouts or 503s from Cloudfront when the Lambda throws the exception. Happens every now and then, other times it will serve the site.

Expected behavior

Serve the site.

Steps to reproduce

Hard to say, all I do is visit the website through Chrome and it occurs. Seems to happen after a lot of inactivity (maybe a cold start problem?)

Screenshots/Code/Logs

Here’s an image of the frequency of the error: https://imgur.com/8TUkz2K

In the cloudwatch logs the lambda outputs the following Invoke Error:

{
    "errorType": "TypeError",
    "errorMessage": "Cannot read property '0' of undefined",
    "stack": [
        "TypeError: Cannot read property '0' of undefined",
        "    at isOriginResponse (/var/task/index.js:5623:25)",
        "    at Runtime.handler$1 [as handler] (/var/task/index.js:5390:9)",
        "    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
    ]
}

And this leads to this line of code in the lambda:

const isOriginResponse = (event) => {
    return event.Records[0].cf.config.eventType === "origin-response";
};

Which tracks back to this line in the src: https://github.com/serverless-nextjs/serverless-next.js/blob/05fb0ebdf38096fb7e0427956c4747e782c680a8/packages/libs/lambda-at-edge/src/default-handler.ts#L568

I believe that the event.Records property is undefined and this is causing the exception.

Versions

Checklist

  • [X ] You have reviewed the README and FAQs, which answers several common questions.
  • [X ] Please first try using the latest @sls-next/serverless-component release version, which may have already fixed your issue. Note that the old serverless-next.js component and the serverless-next.js plugin are deprecated and no longer maintained.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
jtrnegicommented, Oct 28, 2021

that documentation is not giving enough idea that what was done I am facing the same issue , how do i resolve it ?

0reactions
vafPERcommented, Sep 5, 2022

@jmrapp1 Can you explain in more details please how you solved this issue?

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: Cannot read Property '0' of Undefined in JS
The "Cannot read property '0' of undefined" error occurs when trying to access the 0th index in a variable that stores an undefined...
Read more >
Cannot read property '0' of undefined at exports.handler (/var ...
i have passed the Records in the Event and now I'm getting an another Error TypeError: Cannot read property 's3' of undefined at...
Read more >
How to Prevent the Error: Cannot Read Property '0' of Undefined
A guide on how to prevent the error "cannot Read Property '0' of Undefined", covering techniques such as try, catch, using const over...
Read more >
Cannot read property '0' of undefined aws lambda node.js
I created lambda nodejs version 8.10. That echo `TypeError: Cannot read property '0' of undefined`. Probably this line is incorrect `event. ...
Read more >
Cannot Read Property of Undefined in JavaScript - Rollbar
TypeError : Cannot read property of undefined occurs when a property is read or a function is called on an undefined variable.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found