[英]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.