티스토리 뷰

728x90
반응형

deletedAt 값을 삭제날짜로 하여

soft-delete 방식으로 데이터를 삭제하려는데 다음과 같은 에러를 발생했다.

ERROR [ExceptionsHandler] Cast to date failed for value "[Function: now]" (type function) at path "deletedAt"

CastError: Cast to date failed for value "[Function: now]" (type function) at path "deletedAt" at model.Query.exec (/Users/ek/Documents/github/loveAlakazam/4_MarketService/node_modules/mongoose/lib/query.js:4891:21) at model.Query.Query.then (/Users/ek/Documents/github/loveAlakazam/4_MarketService/node_modules/mongoose/lib/query.js:4990:15) at processTicksAndRejections (node:internal/process/task_queues:96:5)

 

문제 발생 데이터베이스 로직

async deleteProduct(productId: string) {
    await this.productModel.updateOne(
      { _id: productId },
      { deletedAt: Date.now },
    );
}

 

 

물론 soft-delete를 할 수 있는 플러그인도 있지만, 플러그인에 의존하지 않고 한번 직접 수정해보고싶다.

 

NestJs Create CRUD And Mongo AND softDelete

Step1 : nest g res Books

khajehossini.medium.com

 

 

진짜 허무하게도... Date.now() 로 고치니까 soft-delete가 됐다

async deleteProduct(productId: string) {
    await this.productModel.updateOne(
      { _id: productId },
      { deletedAt: Date.now() }, // <- 고친부분
    );
}

 

[참고]

 

Possibly unhandled CastError: Cast to date failed for value "function now() { [native code] } on Mongoose

I'm getting the following error when running Express app Possibly unhandled CastError: Cast to date failed for value "function now() { [native code] } The date field is defined on model as:

stackoverflow.com

 

728x90
반응형
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함