猫鼬 - 无法访问 createdAt
Mongoose - can't access createdAt
我正在使用 Mongoose 创建一个 NestJs 应用程序,我目前在尝试访问 createdAt 时遇到问题,即使我已将时间戳设置为 true,我的代码如下。
product.schema.ts
@Schema({timestamps: true})
export class Product extends Document {
@Prop({ required: true })
name!: string;
}
product.service.ts
public async getProduct(name: string): Promise<void> {
const existingProduct = await this.productModel.findOne({ name });
if (!existingProduct) {
throw new NotFoundException();
}
existingProduct.createdAt //Property 'createdAt' does not exist on type 'Product'
}
仅仅因为你设置了 timestamps: true
并不意味着打字稿理解这些字段应该存在。您需要将它们添加到 class,但没有 @Prop()
装饰器,以便打字稿知道字段存在,但 Nest 不会尝试为您重新创建字段。
我正在使用 Mongoose 创建一个 NestJs 应用程序,我目前在尝试访问 createdAt 时遇到问题,即使我已将时间戳设置为 true,我的代码如下。
product.schema.ts
@Schema({timestamps: true})
export class Product extends Document {
@Prop({ required: true })
name!: string;
}
product.service.ts
public async getProduct(name: string): Promise<void> {
const existingProduct = await this.productModel.findOne({ name });
if (!existingProduct) {
throw new NotFoundException();
}
existingProduct.createdAt //Property 'createdAt' does not exist on type 'Product'
}
仅仅因为你设置了 timestamps: true
并不意味着打字稿理解这些字段应该存在。您需要将它们添加到 class,但没有 @Prop()
装饰器,以便打字稿知道字段存在,但 Nest 不会尝试为您重新创建字段。