I have NextJS project with Mongoose schema inside of it. I keep getting following error on dev environment:
TypeError: Cannot read properties of undefined (reading 'Village')
My model file is:
import mongoose from "mongoose";
const {Schema} = mongoose;
const VillageSchema = new mongoose.Schema(
{
id: {type: String},
...
);
// prettier-ignore
export default mongoose.models.Village || mongoose.model("Village", VillageSchema);
CodePudding user response:
Try to add optional chaining to the exported value:
// prettier-ignore
export default mongoose.models?.Village || mongoose.model("Village", VillageSchema);