6

In my nest application I am getting type error when calling _id on user because mongoose defines the _id automatically & therefore its not present in my schema which is defined as type for the promise.

When the promise type is changed to any like Promise<any> then there is no type error.

async create(createUserDto: CreateUserDto): Promise<User> {
    const createdUser = await new this.userModel(createUserDto).save();
    return createdUser;
  }

but I want to know is this the correct way or I should be doing something else.
I do not want to define _id in schema to solve this issue.

 @Prop({ auto: true})
 _id!: mongoose.Types.ObjectId;

user.schema.ts

// all the imports here....

export type UserDocument = User & Document;

@Schema({ timestamps: true })
export class User {

  @Prop({ required: true, unique: true, lowercase: true })
  email: string;

  @Prop()
  password: string;

}

export const UserSchema = SchemaFactory.createForClass(User);   

users.controller.ts

@Controller('users')
@TransformUserResponse(UserResponseDto)
export class UsersController {
  constructor(private readonly usersService: UsersService) {}

  @Post()
  async create(@Body() createUserDto: CreateUserDto) {
      const user = await this.usersService.create(createUserDto);
      return user._id;
  }

}

users.service.ts

// all the imports here....  
   
@Injectable()
export class UsersService {
  constructor(@InjectModel(User.name) private userModel: Model<UserDocument>) {}

  async create(createUserDto: CreateUserDto): Promise<User> {
    const createdUser = await new this.userModel(createUserDto).save();
    return createdUser;
  }
}
  • You can use [`Document.prototype.id`](https://mongoosejs.com/docs/api/document.html#document_Document-id) to get the `_id` in string form. In other words use `user.id` instead. – Jake Holzinger May 24 '22 at 00:04
  • It's okay to define the _id in the schema, if the problem is to define the same property to all your schema you could define a base model and extend that model to your models. – Martinez May 24 '22 at 02:19
  • @JakeHolzinger `user.id` is also showing type error like I said because in the User schema `id` is not present. – curlygadfly_090 May 24 '22 at 07:19
  • A typescript error? If you return `UserDocument` in your `UserService` you should have access to the id. The `User` type does not have an id property. – Jake Holzinger May 24 '22 at 16:05
  • Hello i am also facing this problem . did you solved it and how ?? is it possible without manually define _id property? – Mahmudul Hassan May 25 '22 at 08:44
  • @MahmudulHassan I removed the promise type from User to Any and the type error went away. – curlygadfly_090 May 28 '22 at 03:42

1 Answers1

7

Your create service method should return Promise<UserDocument> instead of Promise<User>. UserDocument has the _id property specified.

async create(createUserDto: CreateUserDto): Promise<UserDocument> {
    const createdUser = await new this.userModel(createUserDto).save();
    return createdUser;
  }
Dezzley
  • 1,463
  • 1
  • 13
  • 19