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

Uncaught (in promise) TypeError: globalThis.uuid is not a function #750

Closed
barbalex opened this issue Dec 12, 2023 · 18 comments
Closed

Uncaught (in promise) TypeError: globalThis.uuid is not a function #750

barbalex opened this issue Dec 12, 2023 · 18 comments

Comments

@barbalex
Copy link

barbalex commented Dec 12, 2023

After nuking the db and rebuilding it, migrating and generating the client, I get this error when starting the app:

common.js:1 Uncaught (in promise) TypeError: globalThis.uuid is not a function
    at S2 (common.js:1:1069)
    at he2._getClientId (process.js:62:2512)
    at async he2.start (process.js:1:2461)
    at async b10.startProcess (registry.js:1:1763)

I found this code, that seems to cause the issue: https://github.com/electric-sql/electric/blob/main/clients/typescript/src/util/common.ts#L120

But I do not understand what is happening or what I might be doing wrong. Glad to give you more information, if you can point me to what would be helpful.

@barbalex
Copy link
Author

I don't know if the two things are related but I tried the same process after updating:

  • electric-sql from 0.7.1 to 0.8.1
  • @electric-sql/prisma-generator from 1.1.0 to 1.1.1

Then:

  1. npm run backend:down
  2. npm run backend:start
  3. db:migrate
  4. client:generate

generating now fails with:

alex@pop-os:~/ps$ npm run client:generate

> ps@0.1.0 client:generate
> npm run electric:check && npx electric-sql generate --service http://localhost:5133 --proxy postgresql://prisma:proxy_password@localhost:65432/electric


> ps@0.1.0 electric:check
> node ./check-electric-is-running.js

DATABASE_URL: postgresql://postgres:proxy_password@localhost:65432/ps
Generating Electric client...
Successfully generated Electric client at: ./src/generated/client
Building migrations...
generate command failed: TypeError: Cannot read properties of undefined (reading 'sqliteType')

@kevin-dp
Copy link
Contributor

I don't know if the two things are related but I tried the same process after updating:

* electric-sql from 0.7.1 to 0.8.1

* @electric-sql/prisma-generator from 1.1.0 to 1.1.1

Then:

1. `npm run backend:down`

2. `npm run backend:start`

3. `db:migrate`

4. `client:generate`

generating now fails with:

alex@pop-os:~/ps$ npm run client:generate

> ps@0.1.0 client:generate
> npm run electric:check && npx electric-sql generate --service http://localhost:5133 --proxy postgresql://prisma:proxy_password@localhost:65432/electric


> ps@0.1.0 electric:check
> node ./check-electric-is-running.js

DATABASE_URL: postgresql://postgres:proxy_password@localhost:65432/ps
Generating Electric client...
Successfully generated Electric client at: ./src/generated/client
Building migrations...
generate command failed: TypeError: Cannot read properties of undefined (reading 'sqliteType')

Hi @barbalex, i created a fresh project using npx create-electric-app@latest and double checked to have the same versions of electric-sql and the generator that you are using. Everything works fine on my end. Could you share your Postgres schema such that i can try to reproduce the error?

@barbalex
Copy link
Author

barbalex commented Dec 13, 2023

@kevin-dp
Here is the schema: https://github.com/barbalex/ps/tree/main/db/migrations

By the way: I did not run a new npx create-electric-app@latest, just updated.

@barbalex
Copy link
Author

As this blocks me from developing the app, I am trying to isolate the error.

My app builds 40 tables.

I have reduced the number of tables to find tables whose client generation causes this error.

So far I have found one: https://github.com/barbalex/ps/blob/main/db/migrations/02-accounts.sql#L5

When I only build 01 and 02, this table's owner_user_id field causes the error. Renaming it to user_id solves it.

As there are other tables causing errors I will keep searching.

@barbalex
Copy link
Author

barbalex commented Dec 13, 2023

Another failing table is: https://github.com/barbalex/ps/blob/main/db/migrations/39-fields.sql

There were two offending fields:

  • type text DEFAULT NULL REFERENCES field_types(field_type) ON DELETE CASCADE ON UPDATE CASCADE: had to rename to field_type to make it work
  • widget text DEFAULT NULL REFERENCES widget_types(widget_type) ON DELETE CASCADE ON UPDATE CASCADE: had to rename to widget_type to get it work.

So it seems there is a requirement that all referencing fields need to be named exactly the same as the referenced field.

That would make building self-referencing tables impossible (parent-child relationships).

@barbalex
Copy link
Author

barbalex commented Dec 13, 2023

Now that generating the client works again, I am back to the original error:

common.js:1 Uncaught (in promise) TypeError: globalThis.uuid is not a function
    at S2 (common.js:1:1069)
    at he2._getClientId (process.js:62:2512)
    at async he2.start (process.js:1:2461)
    at async b10.startProcess (registry.js:1:1763)

...which unfortunately also blocks development of the app.

@barbalex
Copy link
Author

So it seems there is a requirement that all referencing fields need to be named exactly the same as the referenced field. That would make building self-referencing tables impossible (parent-child relationships).

Actually I have exactly that in my models: https://github.com/barbalex/ps/blob/main/db/migrations/14-places.sql#L5

And that works?

@barbalex
Copy link
Author

barbalex commented Dec 13, 2023

concerning globalThis.uuid is not a function

I tried:

  • changing globalThis in common.js to self/this/global. Nope.
    The problem is not globalThis but the missing of the uuid function
  • npm i --save uuid. Nope
  • npm i -g uuid. Nope

To be honest I have no idea where common.js is running and thus how to add the uuid function to that environment.

uuid is correctly installed by electric itself:

alex@pop-os:~/ps$ npm why uuid
uuid@9.0.1
node_modules/uuid
  uuid@"^9.0.0" from electric-sql@0.8.2
  node_modules/electric-sql
    electric-sql@"0.8.2" from the root project

This error occurs on my pop os pc and my MacOS notebook.

@kevin-dp
Copy link
Contributor

As this blocks me from developing the app, I am trying to isolate the error.

My app builds 40 tables.

I have reduced the number of tables to find tables whose client generation causes this error.

So far I have found one: https://github.com/barbalex/ps/blob/main/db/migrations/02-accounts.sql#L5

When I only build 01 and 02, this table's owner_user_id field causes the error. Renaming it to user_id solves it.

As there are other tables causing errors I will keep searching.

I tried reproducing your error but didn't manage yet. On a fresh Electric app, i created your users and accounts tables (just the tables without the indexes) and electrified them. Then i generated the client without problems. I'm using electric-sql@0.8.1 and @electric-sql/prisma-generator@1.1.1.

I also copied all your migrations, applied them all on a fresh electric app, and the client builds without any issues.

@barbalex
Copy link
Author

O.k., so I rebuilt my project from a fresh electric app too, and it now works.

Sorry for bugging you and thanks for your help!

@barbalex barbalex reopened this Dec 14, 2023
@barbalex
Copy link
Author

barbalex commented Dec 14, 2023

So after having lunch I returned to my pc and the issue is back 😢
It happens in chrome and firefox.

@kevin-dp
Copy link
Contributor

So after having lunch I returned to my pc and the issue is back 😢 It happens in chrome and firefox.

Just to double check that we are talking about the same.
The issue that is back is the uuid function not being found, right?
I believe @samwillis has a fix for this. I'll ask him to give you an update about the status of that fix.

What about this other issue:

generate command failed: TypeError: Cannot read properties of undefined (reading 'sqliteType')

Does it still happen on your original migrations? I could not reproduce that using the 40 migrations you shared.

@barbalex
Copy link
Author

The issue that is back is the uuid function not being found, right?

Correct. Sorry for mixing up stuff.

Does it still happen on your original migrations?

I renamed the fields to make them work. Would you like me to test with the original names?

@kevin-dp
Copy link
Contributor

The issue that is back is the uuid function not being found, right?

Correct. Sorry for mixing up stuff.

Does it still happen on your original migrations?

I renamed the fields to make them work. Would you like me to test with the original names?

Yes if you could test with the original names. The 40 migrations in the repo you shared, are that the original names or the renamed ones? Because those worked for me.

@barbalex
Copy link
Author

So I did this:

  • using the up-to-date version of my app
  • reverted three fields in the migrations:
    • accounts.user_id > accounts.owning_user_id
    • fields.field_type > fields.type
    • fields.widget_type > fields.widget
  • tested: yes fails with generate command failed: TypeError: Cannot read properties of undefined (reading 'sqliteType')
  • created a new branch from it for you to test with: https://github.com/barbalex/ps/tree/type_error_generating_client

@kevin-dp
Copy link
Contributor

Regarding uuid, @samwillis fixed his issue here 6bbe71a
Perhaps this solves it for you?

@barbalex
Copy link
Author

How can I test it? (the code needs to be compiled, I couldn't just copy it over)

@barbalex
Copy link
Author

O.k., so I converted it from typescript to js and replaced the typescript common.js files code in my node_modules folder.

And it works!

Thanks you guys so much!

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