본문 바로가기

error records

[Nestjs, Mongoose 에러] AllExceptionFilter document must have an _id before saving

반응형

 

 

Nestjs + Mongoose db 로직을 수정하면서 모델을 수정했는데 기존에 잘 되던 create 요청에 에러가 났다. 

 

AllExceptionFilter MongooseError: document must have an _id before saving

 

저장 전 _id 가 반드시 있어야 한다는 것 같다. 모델에 디폴트로 _id가 생성되긴 하지만 모델에 _id에 정의해주었고 @Prop() 을 붙여준 게 문제였다. 

 

@Schema({
  collection: 'blabla',
  timestamps: true,
  versionKey: false,
  read: 'secondaryPreferred',
})
export class blablaModel {
  @Prop() <- 삭제
  _id: Types.ObjectId;

  @Prop()
  name?: string;
  .
  .
  .

 

여기서 @Prop() 만 지워주니 다시 create 가 잘 되었다. 

 

Prop 이 무엇인지 정확하게 몰랐는데 이번 기회에 제대로 이해해보았다. chatGPT가 알려쥼 

 

@Prop() is a decorator provided by the @nestjs/mongoose package used in NestJS for defining a property in a Mongoose schema. It maps the property to a Mongoose schema property and defines the type of the property. The @Prop() decorator can be used with several options, such as unique, required, default, and type.

 

@Prop()은 nestjs에서 유용하게 쓰이는 데코레이터 중 하나로, Mongoose 스키마 안에서 property 를 정의내릴 때 쓰인다. Mongoose 스키마 프로퍼티에 매핑하고 타입도 정의하면서 unique, required, default 등 옵션값을 정할 수도 있다고 한다. 

 



땅큐도 이해하는 chatGPT 

반응형