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

CORS failure results in Uncaught TypeError: Cannot read properties of undefined (reading 'forEach') in recorder.ts #1459

Open
hugonasciutti opened this issue Oct 8, 2024 · 4 comments

Comments

@hugonasciutti
Copy link

Hello,

I encountered the following error with posthog-js version 1.166.2:

recorder.ts:688 Uncaught TypeError: Cannot read properties of undefined (reading 'forEach')

This error occurs when my request fails due to a CORS policy restriction. I’ve attached a screenshot for reference.

image
image

It seems to be here:

const responseHeaders: Headers = {}
res.headers.forEach((value, header) => {
responseHeaders[header] = value
})

@pauldambra
Copy link
Member

Had you been using previous versions of posthog-js without this?

Absolutely wild that the response would be defined but not have headers. Every time I think that computers have let me down for the last time there's something new 😅

@pauldambra
Copy link
Member

What's strange is that we've not had this reported before... are you using other tools that are wrapping fetch?

(the fix is I imagine pretty clear but I'd love to understand the cause in case there's more we need to do!)

@hugonasciutti
Copy link
Author

hugonasciutti commented Oct 8, 2024

This issue may be related to Supertokens, which I’m using in my project. I noticed that Supertokens overrides the fetch and XMLHttpRequest functions to inject custom headers, so it could be contributing to this bug.

This doesn’t affect my production code; I only noticed it in the console while testing a migration to a newer version of Supertokens.

I’ve used previous versions of posthog-js without encountering this issue, likely because I hadn’t triggered a CORS error like this before.

I thought it would be helpful to open this issue in case others run into the same error log. :)

@pauldambra
Copy link
Member

I thought it would be helpful to open this issue in case others run into the same error log. :)

oh, no, absolutely! i'm super pleased you did. my assumption is always that for one person who does open an issue, many people have had the problem and not bothered.

i believe there aren't any recent changes in posthog-js that should contribute here... but that doesn't mean there's nothing for us to do in response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants