简体   繁体   中英

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")

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)

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”

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM