Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

256
Views
La propiedad '_id' no existe en el tipo. Obteniendo un error de tipo al intentar acceder a la propiedad _id como resultado de una promesa en la aplicación nestjs

En mi aplicación Nest, recibo un error de tipo cuando llamo a _id en el user porque mongoose define el _id automáticamente y, por lo tanto, no está presente en mi esquema que se define como tipo para la promesa.

Cuando el tipo de promesa se cambia a cualquiera como Promise<any> , entonces no hay ningún tipo de error.

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

pero quiero saber si esta es la forma correcta o debería estar haciendo otra cosa.
No quiero definir _id en el esquema para resolver este problema.

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

usuario.esquema.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);

usuarios.controlador.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; } }

usuarios.servicio.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; } }
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!