Home > Enterprise >  TypeErrror: at Reflect.getMetaData Nestjs
TypeErrror: at Reflect.getMetaData Nestjs

Time:12-16

I have two Netsjs modules

1-User

2-Admin

The schema for both modules is same. But there's one difference, the user has default role 0 and admin has default 1. As Admin is also called a user, so I want to use a single mongodb collection(users collection) for both of them. Or I want to send both of them in the same collection after signup.

But In the process I'm getting the following error

[Nest] 756 - 15/12/2022, 3:54:34 pm ERROR [ExceptionHandler] TypeError: at Reflect.getMetadata (D:\Noum\Data\CYBRNODE\MAN STACK\Cybrnode-Blog-Backend\Cybr-Blog-Nest-Backend\node_modules\reflect-metadata\Reflect.js:354:23) at DependenciesScanner.isInjectable (D:\Noum\Data\CYBRNODE\MAN STACK\Cybrnode-Blog-Backend\Cybr-Blog-Nest-Backend\node_modules@nestjs\core\scanner.js:302:26)

Things that I'm doing to achieve to goal

user.module.ts

@Module({
  imports: [
    forwardRef(() => {
      AdminModule;
    }),
    MongooseModule.forFeature([
      {
        name: User.name,
        schema: userSchema,
      },
    ]),
  ],
  controllers: [UserController],
  providers: [UserService, AdminService],
  exports: [UserService],
})
export class UserModule {}

admin.module.ts

@Module({
  imports: [
    forwardRef(() => {
      UserModule;
    }),
  ],
  controllers: [AdminController],
  providers: [AdminService, UserService],
  exports: [AdminService],
})
export class AdminModule {}

admin.service.ts As I want to use the User collection in mongodb, so here I'm injecting UserModel into AdminServcie

@Injectable()
export class AdminService {
  constructor(
    @InjectModel(User.name) private readonly adminModel: Model<userDocument>,
  ) {}

}

I know the logic might have so many counter questions I'll accept all the questions and consider them as well. But first I want to resolve this Reflect.getMetadata issue...

CodePudding user response:

You have bug in:

forwardRef(() => {
    AdminModule;
}),
forwardRef(() => {
    UserModule;
}),

Just replace with:

forwardRef(() => {
    return AdminModule;
})
forwardRef(() => {
    return UserModule;
}),

UPDATED user module:

@Module({
    imports: [
        forwardRef(() => {
            return AdminModule;
        }),
        MongooseModule.forFeature([
            {
                name: User.name,
                schema: UserSchema
            },
        ]),
    ],
    controllers: [],
    providers: [UserService, AdminService],
    exports: [
        UserService,
            MongooseModule.forFeature([
            {
                name: User.name,
                schema: UserSchema
            },
        ]),
    ],
})

Nest MongoDB

If you also want to use the models in another module, add MongooseModule to the exports section of CatsModule and import CatsModule in the other module.

CodePudding user response:

It looks like you are trying to use the forwardRef function in your module definitions in a way that is not supported by Nest.js.

The forwardRef function is used to reference a module that hasn't been defined yet, but you are using it to reference a module that has already been defined. Instead of using forwardRef, you should simply import the other module using the import keyword.

Here is how you can fix the issue:

user.module.ts

@Module({
  imports: [
    AdminModule,  // import the AdminModule instead of using forwardRef
    MongooseModule.forFeature([
      {
        name: User.name,
        schema: userSchema,
      },
    ]),
  ],
  controllers: [UserController],
  providers: [UserService, AdminService],
  exports: [UserService],
})
export class UserModule {}

admin.module.ts

@Module({
  imports: [
    UserModule,  // import the UserModule instead of using forwardRef
  ],
  controllers: [AdminController],
  providers: [AdminService, UserService],
  exports: [AdminService],
})
export class AdminModule {}

After making these changes, the Reflect.getMetadata error should be resolved.

However, there may be other issues with your code that could cause it to fail. For example, it is not clear why you are trying to inject the UserModel into the AdminService, as the AdminService is defined in the AdminModule, and the UserModel is defined in the UserModule. You should make sure that your code is organized and structured in a way that makes sense and is easy to understand.

  • Related