尚未为模型“ResourceSchema”注册架构。 - 嵌套js
Posted
技术标签:
【中文标题】尚未为模型“ResourceSchema”注册架构。 - 嵌套js【英文标题】:Schema hasn't been registered for model "ResourceSchema". - nest js 【发布时间】:2019-12-17 06:20:35 【问题描述】:大家好,我正在尝试使用 Nest js 和 mongodb 构建 api。
我正在尝试在架构之间建立关系,当我尝试从角色填充资源时出现错误
[Nest] 12308 - 2019-08-09 4:22 PM [ExceptionsHandler] Schema hasn't been registered for model "ResourceSchema".
Use mongoose.model(name, schema) +6998ms
MissingSchemaError: Schema hasn't been registered for model "ResourceSchema".
Use mongoose.model(name, schema)
我的角色架构
import * as mongoose from 'mongoose';
import ResourceModel from './resourceSchema';
const Schema = mongoose.Schema;
export const RoleSchema = new Schema(
name:
type: String,
unique: true,
required: [true, 'Role name is required'],
,
resources: [
type: mongoose.Schema.Types.ObjectId,
ref: 'ResourceModel',
],
permissions: [type: String],
);
我的资源架构
import * as mongoose from 'mongoose';
const Schema = mongoose.Schema;
export const ResourceSchema = new Schema(
name:
type: String,
unique: true,
required: [true, 'Role type is required'],
,
routingInfo: type: String,
iconName: type: String,
iconType: type: String,
subResources: [type: mongoose.Schema.Types.Mixed, ref: 'ResourceModel'],
);
export const ResourceModel = mongoose.model('Resource', ResourceSchema);
我的角色 service.ts 填充资源数组
...
@Injectable()
export class RoleService
constructor(@InjectModel('roles') private readonly roleModel: Model<Role>)
async findAll(): Promise<Role[]>
return await this.roleModel.find().populate(path: 'resources', Model: ResourceModel);
// also tried that
async findAll(): Promise<Role[]>
return await this.roleModel.find().populate(path: 'resources', Model: ResourceSchema);
我的资源模块
import Module from '@nestjs/common';
import ResourcesController from './resources.controller';
import ResourcesService from './resources.service';
import MongooseModule from '@nestjs/mongoose';
import ResourceSchema from '../schemas/resourceSchema';
import ConfigService from '../config/config.service';
import AuthService from '../auth/auth.service';
import UsersService from '../users/users.service';
import UserSchema from '../schemas/userSchema';
@Module(
imports: [MongooseModule.forFeature([name: 'users', schema: UserSchema]),
MongooseModule.forFeature([name: 'resources', schema: ResourceSchema])],
providers: [ResourcesService, UsersService, AuthService, ConfigService],
controllers: [ResourcesController],
exports: [ResourcesService],
)
export class ResourcesModule
所以当我对邮递员执行 GET REQUEST 时,我得到了那个错误 谁能告诉我我做错了什么??????
感谢和vaced
【问题讨论】:
【参考方案1】:ResourceSchema
是模式,而不是模型。您需要为模型注册它,例如:
export const Resource = mongoose.model('Resource', ResourceSchema);
更改参考:
subResources: [type: mongoose.Schema.Types.Mixed, ref: 'Resource'],
他们将其导入您的服务并调用:
return await this.roleModel.find().populate(path: 'resources', Model: Resource );
希望对您有所帮助。
【讨论】:
和裁判?应该仍然是Schema?仍然给我错误 啊,你也应该改变 ref。我忘记了 更改参考“资源”? 还应该更改 RoleSchema 中的资源。像魔术一样工作 我对猫鼬没有太多经验,但在文档中它说Note: ObjectId, Number, String, and Buffer are valid for use as refs. However, you should use ObjectId unless you are an advanced user and have a good reason for doing so
。以上是关于尚未为模型“ResourceSchema”注册架构。 - 嵌套js的主要内容,如果未能解决你的问题,请参考以下文章
致命错误:尚未为模型“a”注册架构。使用 mongoose.model(name, schema)