#Mongoose InjectModel weird error

22 messages · Page 1 of 1 (latest)

frozen sierra
#

This is the code

#
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { User, UserDocument } from 'src/schemas/user.schema';

@Injectable()
export class UserService {
  constructor(
    @InjectModel(User.name) private readonly userModel: Model<UserDocument>,
  ) {}
}
#
Unable to resolve signature of parameter decorator when called as an expression.
  Argument of type 'undefined' is not assignable to parameter of type 'string | symbol'.ts(1239)
#

This error is on injectModel, I legit copypasted docs and it gives this weird error

#

Folder structure

#
import { Prop,Schema, SchemaFactory } from '@nestjs/mongoose';
import { HydratedDocument } from 'mongoose';

export type UserDocument = HydratedDocument<User>;

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

export const UserSchema = SchemaFactory.createForClass(User);

user.schema.ts

#
import { Module } from '@nestjs/common';
import { UserService } from './user.service';
import { UserController } from './user.controller';
import { MongooseModule } from '@nestjs/mongoose';
import { User, UserSchema } from 'src/schemas/user.schema';

@Module({
  imports: [
    MongooseModule.forFeature([{ name: User.name, schema: UserSchema }]),
  ],
  controllers: [UserController],
  providers: [UserService],
})
export class UserModule {}

user.module.ts

frail dawn
#

For me, it looks like its a typescript problem. Are you using any typescript extension?

frozen sierra
#

none

#

I have been trying to fix this error for 3 hours

#

it is driving me crazy

frail dawn
#

Can you open vscode, hit CTRL+SHIFT+P and type "TypeScript: Select TypeScript version"

#

an send me a screenshot

frozen sierra
#

@frail dawn

frail dawn
#

Can you select workspace version and test again?

frozen sierra
#

I hate typescript

#

it works now

#

and I wasted 3 hours

#

on this

frail dawn
#

all of us already passed that type of situation 😅

frozen sierra
#

thank you a lot man