//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)
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 })
Most helpful comment
did you already try to make the
ref:calls as strings?https://typegoose.github.io/typegoose/guides/advanced/reference-other-classes/