![](/img/trans.png)
[英]Mongoose getting the error: MissingSchemaError: Schema hasn't been registered for model
[英]Mongoose error MissingSchemaError: Schema hasn't been registered for model in typescript
花费大量时间在互联网上进行研究。 在这种情况下,我无法找出我遗漏了什么。 请帮助我,非常感谢!
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(这是模块 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 };
我在路线中使用过:
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);
错误
“Mongoose 错误 MissingSchemaError:尚未为 model 模块注册架构”
我今天的所有时间都用来找出问题,但不能。 请帮帮我,我错过了什么?
解决方法是在路由开始时导入模块文件。
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);
解释是您需要执行以下行来注册Module
model 的模式:
const Module = mongoose.model<ModuleDoc, ModuleModel>('Module', ModuleSchema);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.