在更新时不更新使用带有Nestjs的时间戳

问题描述

我正在将NestJs用于一个后端项目,并试图使用时间戳来显示更新和创建日期,但是什么也没有显示!

    @Schema()
export class Camera extends Document{
 
//   @Prop({required: true,unique: true})
  @Prop({required: true})
  facility_name: string;

  @Prop({required: true,unique : true})
  camera_id: string;
  
  @Prop({required: true})
  camera_location: string;

  @Prop({required: true})
  camera_type: string;

  @Prop({default : false})
  is_deleted : boolean;

  @Prop()
  timestamps: true;

}

export const cameraSchema = SchemaFactory.createForClass(Camera); }

我该如何使用带有时间戳的frmawork时间戳,因为它没有显示任何日期!

解决方法

@Schema装饰器接受模式选项对象作为参数:

@Schema({
  timestamps: true,})

使用此选项,会将 createdAt updatedAt 属性添加到集合的文档中:

{
  "_id": "5fc3fab191c59905a0931df2","content": "Lorem ipsum dolor sit amet","createdAt": "2020-11-29T19:46:57.199Z","updatedAt": "2020-11-29T19:46:57.199Z","__v": 0
}

https://mongoosejs.com/docs/guide.html#options上查看有关所有选项的扩展信息

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...