我有NextJS项目与猫鼬模式里面。我一直在开发环境中得到以下错误:
我的模型文件是:TypeError: Cannot read properties of undefined (reading 'Village')
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);
尝试为导出值添加可选链接:
// prettier-ignore
export default mongoose.models?.Village || mongoose.model("Village", VillageSchema);