I am trying to implement an application using nestJs using nx monorepo concept. I ran into one issue which is a bit strange, I can't figure it out how this is happening like this..

I have a organisation schema which is registered in organisation Module, I also have an auth module with userschema and products module productschema registered..

Recently I changed the data model to have mongo db collection for organisation to have embedded products and embedded users.

this change made it mandatory that I have to import the schemas defined in the products and auth to be imported into the organisation module as the org schema no will have products and users as subdocuments

import { User, UserSchema } from '@jafar-tech/backend/auth';
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Product, ProductSchema } from '@jafar-tech/backend/products';

import { Document } from 'mongoose';

@Schema()
export class Organisation extends Document {
  @Prop()
  name: string;
  @Prop()
  caption: string;
  @Prop()
  type: string[];
  @Prop()
  address: string;
  @Prop()
  coord: string[];

  @Prop()
  license: string;

  @Prop({ default: true })
  openAllWeek: boolean;

  @Prop()
  offDays: string[];

  @Prop({ type: [ProductSchema], default: [] })
  product: Product[];

  @Prop({ type: [UserSchema], default: [] })
  users: User[];
}

export const OrganisationSchema = SchemaFactory.createForClass(Organisation);

If I keep this schema like this. I get this error for this line of code..

code:

   @Injectable()
export class ProductsRepository {
  constructor(
    @InjectModel(Organisation.name) orgModel: Model<Organisation>,
    @InjectModel(Product.name)
    private readonly product: Model<Product>
  ) {

Organisation.name is the culprit..this is the error.

/home/jafar/nx/jafar-tech/dist/apps/table-qr-api/webpack:/jafar-tech/libs/backend/products/src/lib/products.repository.ts:10
    @InjectModel(Organisation.name) orgModel: Model<Organisation>,
                              ^
TypeError: Cannot read properties of undefined (reading 'name')
    at Object../libs/backend/products/src/lib/products.repository.ts (/home/jafar/nx/jafar-tech/dist/apps/table-qr-api/webpack:/jafar-tech/libs/backend/products/src/lib/products.repository.ts:10:31)

Strange thing I have the same dependency injection for organisation.name in the auth module but that doesn't show this error.

The real question is : When I try to make the productSchema import as the absolute without @nx/nrwl way of importing with tsconfig path map. It seems to work with no error.

import { User, UserSchema } from '@jafar-tech/backend/auth';
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import {
  Product,
  ProductSchema,
} from 'libs/backend/products/src/lib/models/product.schema';

import { Document } from 'mongoose';

@Schema()
export class Organisation extends Document {
  @Prop()
  name: string;
  @Prop()
  caption: string;
  @Prop()
  type: string[];
  @Prop()
  address: string;
  @Prop()
  coord: string[];

look at the above organisation schema.. Here when I import the product and product schema using the absolute path. It works with no error.

Questions:

  1. Any idea why absolute path works and not the @ import.
  2. If you look at the auth module import in the schema which is @ import and which also has the organisation.name injected into one of its repository, but that works well without the absolute path.??

I am stuck at this problem.. plz help

0

There are 0 best solutions below