简体   繁体   English

ZCCADDEDB567ABAE643E15DCF0974E503Z 错误 MissingSchemaError: Schema 尚未为 typescript 中的 model 注册

[英]Mongoose error MissingSchemaError: Schema hasn't been registered for model in typescript

With many time spend for researching on internet.花费大量时间在互联网上进行研究。 I cannot find out what does I missing in this case.在这种情况下,我无法找出我遗漏了什么。 Please help me, thank you so much!请帮助我,非常感谢!

Permission.ts (This's Permission model file. It have refs with Module model by "ModelID") Permission.ts(这是 Permission model 文件。它通过“ModelID”与模块 model 有参考)

import mongoose from 'mongoose';

// An interface that describes the properties
// that are requried to create a new User
interface PermissionAttrs {
  Code: string;
  Name: string;
  Description: string;
  ModuleID: string;
  Active: boolean;
  CreatedByUserID: string;
  UpdatedByUserID: string;
  createdAt: Date;
  updatedAt: Date;
}

// An interface that describes the properties
// that a User Model has
interface PermissionModel extends mongoose.Model<PermissionDoc> {
  build(attrs: PermissionAttrs): PermissionDoc;
}

// An interface that describes the properties
// that a User Document has
interface PermissionDoc extends mongoose.Document {
  Code: string;
  Name: string;
  Description: string;
  ModuleID: string;
  Active: boolean;
  CreatedByUserID: string;
  UpdatedByUserID: string;
  createdAt: Date;
  updatedAt: Date;
}

const PermissionSchema = new mongoose.Schema(
  {
    Code: { type: String, required: true, unique: true },
    Name: { type: String, required: true },
    Description: { type: String },
    ModuleID: { type: mongoose.Schema.Types.ObjectId, ref: 'Module' },
    Active: { type: Boolean, default: true },
    CreatedByUserID: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
    UpdatedByUserID: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
    createdAt: { type: Date, default: Date.now() },
    updatedAt: { type: Date },
  },
  {
    timestamps: true,
    toJSON: {
      transform(doc, ret) {
        ret.id = ret._id;
        delete ret._id;
        delete ret.__v;
      },
    },
  }
);

PermissionSchema.statics.build = (attrs: PermissionAttrs) => {
  return new Permission(attrs);
};

const Permission = mongoose.model<PermissionDoc, PermissionModel>(
  'Permission',
  PermissionSchema
);

export { Permission };

Module.ts (This's Module model file) Module.ts(这是模块 model 文件)

import mongoose from 'mongoose';

// An interface that describes the properties
// that are requried to create a new User
interface ModuleAttrs {
  Name: string;
  Description: string;
  Active: boolean;
  CreatedByUserID: string;
  UpdatedByUserID: string;
  createdAt: Date;
  updatedAt: Date;
}

// An interface that describes the properties
// that a User Model has
interface ModuleModel extends mongoose.Model<ModuleDoc> {
  build(attrs: ModuleAttrs): ModuleDoc;
}

// An interface that describes the properties
// that a User Document has
interface ModuleDoc extends mongoose.Document {
  Name: string;
  Description: string;
  Active: boolean;
  CreatedByUserID: string;
  UpdatedByUserID: string;
  createdAt: Date;
  updatedAt: Date;
}

const ModuleSchema = new mongoose.Schema(
  {
    Name: { type: String, required: true },
    Description: { type: String },
    Active: { type: Boolean, default: false },
    CreatedByUserID: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
    UpdatedByUserID: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
    createdAt: { type: Date, default: Date.now() },
    updatedAt: { type: Date },
  },
  {
    timestamps: true,
    toJSON: {
      transform(doc, ret) {
        ret.id = ret._id;
        delete ret._id;
        delete ret.__v;
      },
    },
  }
);

ModuleSchema.statics.build = (attrs: ModuleAttrs) => {
  return new Module(attrs);
};

const Module = mongoose.model<ModuleDoc, ModuleModel>('Module', ModuleSchema);

export { Module };

I used in route:我在路线中使用过:

import { Permission } from '../models/permission';
...
const Items = await Permission.find()
        .populate('ModuleID')
        .or(filter || {})
        .sort(sort || {})
        .skip(start || 0)
        .limit(length || 0);

      console.log(Items);

Error错误

“Mongoose error MissingSchemaError: Schema hasn't been registered for model Module” “Mongoose 错误 MissingSchemaError:尚未为 model 模块注册架构”

I spend all time of today to find out issue but cannot.我今天的所有时间都用来找出问题,但不能。 Please help me, what's I missing?请帮帮我,我错过了什么?

The fix is to import the Module file at the beginning of your route.解决方法是在路由开始时导入模块文件。

import { Module } from '../models/Module';
import { Permission } from '../models/permission';
...
const Items = await Permission.find()
        .populate('ModuleID')
        .or(filter || {})
        .sort(sort || {})
        .skip(start || 0)
        .limit(length || 0);

      console.log(Items);

The explanation is that you need the following line to be executed to register the schema for the Module model:解释是您需要执行以下行来注册Module model 的模式:

const Module = mongoose.model<ModuleDoc, ModuleModel>('Module', ModuleSchema);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Mongoose 收到错误:MissingSchemaError: Schema has not been registered for model - Mongoose getting the error: MissingSchemaError: Schema hasn't been registered for model throw new mongoose.Error.MissingSchemaError(name) MissingSchemaError: Schema还没有为model“superheros”注册 - throw new mongoose.Error.MissingSchemaError(name) MissingSchemaError: Schema hasn't been registered for model “superheros” MissingSchemaError:尚未为模型注册架构 - MissingSchemaError: Schema hasn't been registered for model MissingSchemaError:尚未为模型注册架构 - MissingSchemaError: Schema hasn't been registered for model MissingSchemaError:尚未为模型注册架构 - MissingSchemaError: Schema hasn't been registered for model 猫鼬插件抛出错误:MissingSchemaError:尚未为模型“约会”注册架构 - mongoose plugin throws an error: MissingSchemaError: Schema hasn't been registered for model “Appointment” MissingSchemaError:尚未为模型“用户,userSchema”注册架构。 使用 mongoose.model(name, schema) - MissingSchemaError: Schema hasn't been registered for model "User, userSchema". Use mongoose.model(name, schema) MissingSchemaError:尚未为模型“ Marker”注册架构 - MissingSchemaError: Schema hasn't been registered for model “Marker” MissingSchemaError:尚未使用 execPopulate 为 model 注册架构 - MissingSchemaError: Schema hasn't been registered for model using execPopulate MissingSchemaError:尚未为模型“ Post”注册架构 - MissingSchemaError: Schema hasn't been registered for model “Post”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM