Skip to content

Instantly share code, notes, and snippets.

@Lahirutech
Created December 8, 2023 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lahirutech/ebd5a17d56343db690b263865812a6dc to your computer and use it in GitHub Desktop.
Save Lahirutech/ebd5a17d56343db690b263865812a6dc to your computer and use it in GitHub Desktop.
import { Document, Model } from "mongoose";
import * as Mongoose from "mongoose";
const postSchema = new Mongoose.Schema({
name: {
type: String,
required: true,
},
});
interface IPost {
name: string;
}
interface IPostDocument extends IPost, Document {}
interface IPostModel extends Model<IPostDocument> {}
// Model export that creates error
// const PostModel: IPostModel = Mongoose.model<IPostDocument>("post", postSchema);
//Solution
const PostModel: IPostModel =
Mongoose.models.post || Mongoose.model<IPostDocument>("post", postSchema);
export default PostModel;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment