I want create simply ecommerce project in nextjs and mongoose. I have error on early begning during creating models.
I have simply model like this
import mongoose, { Schema } from 'mongoose';
export interface IProduct {
name: string;
price: number;
}
const productSchema = new Schema<IProduct>({
name: {
type: String,
},
price: {
type: Number,
},
});
export default mongoose.model<IProduct>('product', productSchema);
After runing project i gettint error OverwriteModelError: Cannot overwrite
product model once compiled.
So i changed export to export default mongoose.models.product || mongoose.model<IProduct>('product', productSchema);
.
After that everything seems to work but my models lost types sugestion and instead of
const products: Product[] = await Product.find({});
i have
const products: any[] = await Product.find({});