I'm creating a NestJs application using Mongoose, I'm currently having a problem trying to access createdAt even though I have set timestamps to true, my code is below.
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'
}
Just because you set timestamps: true doesn't mean that typescript understands that those fields should exist. You need to add them to the class, but without the @Prop() decorator, so that typescript knows the fields exist, but Nest doesn't try to re-create the fields for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With