花費大量時間在互聯網上進行研究。在這種情況下,我無法找出我遺漏了什么。請幫助我,非常感謝!
Permission.ts(這是 Permission 模型檔案。它通過“ModelID”與 Module 模型具有參考)
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(這是模塊模型檔案)
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:尚未為模型模塊注冊模式”
我今天的所有時間都用來找出問題,但不能。請幫幫我,我錯過了什么?
uj5u.com熱心網友回復:
解決方法是在路由開始時匯入模塊檔案。
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模型的架構:
const Module = mongoose.model<ModuleDoc, ModuleModel>('Module', ModuleSchema);
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/414595.html
標籤:
