i'm not used to typescript, i get this error when I try to run my app.
the error:
src/controller/index.controller.ts:6:11 - error TS2740: Type '(IProduct & { _id: any; })[]' is missing the following properties from type 'IProduct': title, description, price, $getAllSubdocs, and 49 more. 6 const product: IProduct = await Product.find();
The code:
model/Product.ts
import { Schema, model, Document } from 'mongoose';
export interface IProduct extends Document {
title: string;
description: string;
price: number;
}
const productSchema = new Schema(
{
title: {
type: String,
required: true,
minlength: [4, 'Please insert a correct value'],
trim: true
},
description: {
type: String,
required: true,
minlength: [10, 'Please insert a correct value'],
trim: true
},
price: {
type: Number,
required: true
}
},
{ timestamps: true, versionKey: false }
);
export default model<IProduct>('Product', productSchema);
controller/index.controller.ts
import { Request, Response } from 'express';
import Product, { IProduct } from '../model/Product';
export const products = async (req: Request, res: Response): Promise<void | Response> => {
try {
const product: IProduct = await Product.find();
return res.status(200).json(product);
} catch (error) {
console.error(error);
return res.status(500).json({ msg: 'Something is wrong' });
}
};