TypeORM OneToMany causes “ReferenceError: Cannot access '' before initialization”

后端 未结 3 1503
甜味超标
甜味超标 2021-01-01 02:36

I have two entities: User and Habit. A user can create multiple Habits, thus I use a OneToMany relation on the User (and

3条回答
  •  旧巷少年郎
    2021-01-01 02:49

    I solved this by using autoLoadEntities: true when loading the TypeORM config to NestJS. Note that this is a NestJS extra, so if you are using ormconfig.json this property won't be applied.

    autoLoadEntities documentation here: https://docs.nestjs.com/techniques/database#auto-load-entities

    UPDATE 04/10/2020

    I kept having the same issue with relations. Another solution I found, even if it breaks some standards is to add all the Entities into 1 file. Export them there and import them where needed.

    Keep in mind that the order in which the classes are declared matters.

提交回复
热议问题