Typegoose: Models ref each other error: circular dependencies problem.

Created on 21 Oct 2019  路  5Comments  路  Source: typegoose/typegoose

//group.model.ts
import { prop, Ref } from '@typegoose/typegoose'
import { User } from './user.model'

export class Group {
  @prop()
  title: string

  @prop({ref: User})
  user: Ref<User>
}

//user.model.ts
import { prop, Ref } from '@typegoose/typegoose'
import { Group } from './group.model'

export class User {
  @prop({ref: Group})
  group: Ref<Group>
}

Error stack:

[Nest] 10640   - 2019-10-21 15:45:20   [ExceptionHandler] Invalid ref at path "user". Got undefined +24ms
MongooseError: Invalid ref at path "user". Got undefined
    at new MongooseError (C:\work\code\nest-test\node_modules\mongoose\lib\error\mongooseError.js:10:11)
    at validateRef (C:\work\code\nest-test\node_modules\mongoose\lib\helpers\populate\validateRef.js:17:9)
    at Schema.path (C:\work\code\nest-test\node_modules\mongoose\lib\schema.js:577:5)
    at Schema.add (C:\work\code\nest-test\node_modules\mongoose\lib\schema.js:442:12)
    at new Schema (C:\work\code\nest-test\node_modules\mongoose\lib\schema.js:122:10)
    at Object._buildSchema (C:\work\code\nest-test\node_modules\@typegoose\typegoose\lib\internal\schema.js:42:15)
    at buildSchema (C:\work\code\nest-test\node_modules\@typegoose\typegoose\lib\typegoose.js:126:20)
    at Object.getModelForClass (C:\work\code\nest-test\node_modules\@typegoose\typegoose\lib\typegoose.js:85:44)
    at InstanceWrapper.useFactory [as metatype] (C:\work\code\nest-test\node_modules\nestjs-typegoose\dist\typegoose.providers.js:21:49)       
    at Injector.instantiateClass (C:\work\code\nest-test\node_modules\@nestjs\core\injector\injector.js:279:55)
    at callback (C:\work\code\nest-test\node_modules\@nestjs\core\injector\injector.js:74:41)
    at processTicksAndRejections (internal/process/task_queues.js:85:5)
    at async Injector.resolveConstructorParams (C:\work\code\nest-test\node_modules\@nestjs\core\injector\injector.js:113:24)
    at async Injector.loadInstance (C:\work\code\nest-test\node_modules\@nestjs\core\injector\injector.js:78:9)
    at async Injector.loadProvider (C:\work\code\nest-test\node_modules\@nestjs\core\injector\injector.js:35:9)
    at async Promise.all (index 4)
bug

Most helpful comment

did you already try to make the ref: calls as strings?

https://typegoose.github.io/typegoose/guides/advanced/reference-other-classes/

All 5 comments

did you already try to make the ref: calls as strings?

https://typegoose.github.io/typegoose/guides/advanced/reference-other-classes/

@hasezoey Thanks a lot, it works.

I got a related error,

Cannot access 'Group' before initialization

But I don't see those strings in the typegoose code or mongoose code... :man_shrugging:

It was fixed by putting 'Group' in a string.

But I don't see those strings in the typegoose code or mongoose code...

@dandv what do you mean with this statement?


-> https://typegoose.github.io/typegoose/guides/advanced/reference-other-classes/#common-problems

Dynamic import solves the problem

@prop({ ref: async ()=> (await import('./group.model')).Group })

Was this page helpful?
0 / 5 - 0 ratings

Related issues

remidej picture remidej  路  7Comments

kerolloz picture kerolloz  路  6Comments

richarddd picture richarddd  路  8Comments

AbderrazzakB picture AbderrazzakB  路  8Comments

Negan1911 picture Negan1911  路  10Comments