Stack Exchange
Mongoose Schema is undefined
I have 2 schemas: User and Product, User have array of products and Product have single user. Schemas looks like:
User:
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Schema as mongooseSchema } from 'mongoose';

import { Product } from '../product/product.schema';

export type UserDocument = User & Document;

@Schema()
export class User {
  @Prop()
  username: string;

  @Prop({ type: [{ type: mongooseSchema.Types.ObjectId, ref: Product }] })
  product: Product[];
}

export const UserSchema = SchemaFactory.createForClass(User);


Product:
import { Document, Schema as mongooseSchema } from 'mongoose';
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { User } from '../user/user.schema';

export type ProductDocument = Product & Document;

@Schema()
export class Product {
  @Prop()
  name: string;

  @Prop({ type: mongooseSchema.Types.ObjectId, ref: User })
  user: User;
}

export const ProductSchema = SchemaFactory.createForClass(Product);


Product module:
@Module({
  imports: [
    MongooseModule.forFeature([
      { name: User.name, schema: UserSchema },
      { name: Product.name, schema: ProductSchema },
    ]),
    UserModule,
  ],
  providers: [ProductService],
  controllers: [ProductController],
})
export class ProductModule {}

Issue come on compile time:
TypeError: Cannot read property 'name' of undefined in product.schema.js:25
I think there is some async issue, When i try to initialize User Model it sees there a ref to Product model and goes to initialize Product model, but then product model point ref to user model which is not initialized yet. If I remove product: Product[] from User Model compiles successfully
How I'm able to avoid this compile time error, thanks a lot!

Yes in Product schema: import { User } from '../user/user.schema';
And in Product module UserModule is imported as well

Hm this actually looks ok (the way you had it before with Product.name was actually correct, I was wrong about that) - weird, I'll try to reproduce this.

Yea i think there should be way for async loading on ref models but not sure tho

Though I'm not 100% sure, you could try to specify the ref value as a string instead of using the name property of the class. There is probably some issue with circular dependencies:
@Schema()
export class Product {
  @Prop()
  name: string;

  @Prop({ type: mongooseSchema.Types.ObjectId, ref: 'User'})
  user: User;
}


EDIT:
Just got feedback on github: when dealing with circular deps in your schemas one has to use strings as described above.

So i have to create a new schema which combine both refs? I'm able to put ref to products in my user schema and ref to user in my product schema?

I see but the issue is that my User is undefined, MongooseError: Invalid ref at path "user". Got undefined if i use only User without name

Looks like something is wrong with your import then. Are the paths correct?

I think there is some async issue, When i try to initialize User Model it sees there a ref to Product model and goes to initialize Product model, but then product model point ref to user model which is not initialized yet

See my edit - I was able to reproduce this, looks like an actual bug in nestjs/mongoose. I will create an issue on github.

Yes that was my workaround for now as well gotta test it

You got it a issue in github would be awesome :)

-> github issue: https://github.com/nestjs/mongoose/issues/699
