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

Fix "Cannot convert undefined to a BigInt" #8

Merged
merged 5 commits into from
Nov 25, 2022

Conversation

KernelFreeze
Copy link
Contributor

  • I have verified my changes didn't break the example project.

Description

I fixed the following exception when executing queries:

Uncaught (in promise) TypeError: Cannot convert undefined to a BigInt
  insertId: results.lastRowId !== null ? BigInt(results.lastRowId) : undefined,
                                         ^
      at executeQuery

src/index.ts Outdated
@@ -107,7 +107,7 @@ class D1Connection implements DatabaseConnection {
}

return {
insertId: results.lastRowId !== null ? BigInt(results.lastRowId) : undefined,
insertId: !results.lastRowId ? undefined : BigInt(results.lastRowId),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this documented in cloudflare's docs anywhere, do you have a link to it? If so, lets insert a comment?

I'm also concerned about other zero values here, hence the explicit check

Copy link
Contributor Author

@KernelFreeze KernelFreeze Nov 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not documented sadly, but I get a lot of undefineds. I added a new commit that returns the original variable so things like 0 and NaN get returned as is.

@aidenwallis
Copy link
Owner

aidenwallis commented Nov 22, 2022

🤔 Now I'm wondering if 0 should really be BigInt(0) here lol, perhaps results.lastRowId === undefined || results.lastRowId === null ? undefined : BigInt(results.lastRowId)

there is a difference between those two and we should aim to return the correct types

Edit: yep looks like this generates type errors, check the build 👍

@KernelFreeze
Copy link
Contributor Author

Is it ok like this?

@aidenwallis
Copy link
Owner

These changes all make sense to me now, thank you for the contribution!

@aidenwallis aidenwallis merged commit cbd3c97 into aidenwallis:main Nov 25, 2022
@aidenwallis aidenwallis mentioned this pull request Nov 25, 2022
1 task
adam-the pushed a commit to adam-the/kysely-d1 that referenced this pull request Aug 20, 2023
* Fix "Cannot convert undefined to a BigInt"

* Return original variable if conversion to BigInt is not possible

* Only return `undefined` on `null` or `undefined`

Co-authored-by: Aiden <[email protected]>
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

Successfully merging this pull request may close these issues.

2 participants