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

Cannot read properties of undefined (reading 'toLowerCase') (NextJS 11) #2323

Closed
Arslanoov opened this issue Oct 30, 2021 · 3 comments
Closed

Comments

@Arslanoov
Copy link

Hello. Thank you for the library

Describe the bug
When I am trying to initialize orm, this error appears

Stack trace

[discovery] ORM entity discovery started, using ReflectMetadataProvider
[discovery] - processing entity User
error - TypeError: Cannot read properties of undefined (reading 'toLowerCase')
at ReflectMetadataProvider.initPropertyType (/usr/src/app/node_modules/@mikro-orm/core/metadata/ReflectMetadataProvider.js:17:83)
    at /usr/src/app/node_modules/@mikro-orm/core/metadata/ReflectMetadataProvider.js:9:54
    at ReflectMetadataProvider.initProperties (/usr/src/app/node_modules/@mikro-orm/core/metadata/MetadataProvider.js:26:23)
    at async ReflectMetadataProvider.loadEntityMetadata (/usr/src/app/node_modules/@mikro-orm/core/metadata/ReflectMetadataProvider.js:9:9)
    at async MetadataDiscovery.discoverEntity (/usr/src/app/node_modules/@mikro-orm/core/metadata/MetadataDiscovery.js:182:13)
    at async MetadataDiscovery.discoverReferences (/usr/src/app/node_modules/@mikro-orm/core/metadata/MetadataDiscovery.js:131:13)
    at async MetadataDiscovery.findEntities (/usr/src/app/node_modules/@mikro-orm/core/metadata/MetadataDiscovery.js:71:9)
    at async MetadataDiscovery.discover (/usr/src/app/node_modules/@mikro-orm/core/metadata/MetadataDiscovery.js:34:9)
    at async Function.init (/usr/src/app/node_modules/@mikro-orm/core/MikroORM.js:42:24)
    at async handler (webpack-internal:///./src/api/useCases/articles/getAll/handler.ts:20:7) {
  page: '/articles'
}

To Reproduce
Steps to reproduce the behavior:

  1. Clone this repository https://github.com/Arslanoov/red
  2. run make build
  3. run make up
  4. run make migrate
  5. Try to open localhost:3000/articles page

Expected behavior
Page should open

Additional context
Api files: https://github.com/Arslanoov/red/tree/main/app/src/api
Models folder: https://github.com/Arslanoov/red/tree/main/app/src/api/model
ORM config: https://github.com/Arslanoov/red/blob/main/app/src/api/config/mikroOrm.ts
Database init: https://github.com/Arslanoov/red/blob/main/app/src/api/utils/database/init.ts

Versions

Dependency Version
node 16
typescript 4.4.4
mikro-orm 4.5.9
next 11.1.2
reflect-metadata 0.1.13
@Arslanoov
Copy link
Author

I fixed this issue by adding type: 'string' to all enum properties
It's strange, because documentation doesn't mention it
I think I should close this issue
enum
role

@msheakoski
Copy link

Thank you @Arslanoov! I lost many hours trying to figure out if it was a TypeScript or SWC or Babel decorators issue, or something automatic that Next.js was doing. It was working without Next.js but when loading in an API route the "toLowerCase" error started happening.

What worked for me was EnumType:

import { Enum, EnumType } from "@mikro-orm/core"

@Enum({ type: EnumType, items: () => CountryCode })
country!: CountryCode

@superiums
Copy link

same issue.
this is caused by the metaDataDiscovery.js

 getMappedType(prop) {
        this.logger.log('discovery-->',JSON.stringify(prop))
        let t = prop.columnTypes?.[0] ?? prop.type.toLowerCase();
      
...
}

after debug, i found there's no type prop for Enum properties.
so i have to add a type ( tinnyint /smallint/string is ok) for all enum properties. but it would be better to add a default type for it by mikroorm.

this works:

  @Enum({ type: 'tinyint', items: () => ORG_SCOPE, nullable: true })
  orgScope: ORG_SCOPE;

@B4nan B4nan reopened this Jul 9, 2023
@B4nan B4nan closed this as completed in b94048b Jul 14, 2023
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

4 participants