尚未为模型“ResourceSchema”注册架构。 - 巢.js

2024-03-06

大家好,我正在尝试使用 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 {
}

所以当我在邮递员上获取请求时我收到了这个错误 任何人都可以告诉我做错了什么???

提前致谢


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 });

希望能帮助到你。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

尚未为模型“ResourceSchema”注册架构。 - 巢.js 的相关文章

  • 使用 Node.js 将对象写入文件

    我已经在 stackoverflow google 上搜索过这个 但似乎无法弄清楚 我正在抓取给定 URL 页面的社交媒体链接 该函数返回一个包含 URL 列表的对象 当我尝试将此数据写入不同的文件时 它会输出到该文件 object Obj
  • Nodejs + mongodb:如何查询 $ref 字段?

    我将 MongoDB 与 Nodejs REST 服务一起使用 该服务公开了存储在其中的数据 我有一个关于如何查询使用 ref 的数据的问题 这是一个对象的示例 其中包含对花药集合中另一个对象 详细信息 的引用 id ObjectId 59
  • Ionic 2 自定义后退按钮操作

    我想自定义此屏幕截图中提到的后退按钮的单击操作 我希望通过单击我不会返回到上一页 而是返回到我自己指定的页面 或者在返回之前进行处理 要自定义默认后退按钮操作 您需要覆盖返回按钮点击 NavBar 组件的方法 Step 1 在你的 自定义类
  • Typescript 1.8 模块:从文件夹导入所有文件

    我正在使用 Typescript 构建一个大型库 其中包含 100 个独立的 ts 文件 以前我用过导出模块XXX 重命名为导出命名空间 XXX稍后 对于我的所有课程 但正如书籍所说 这不是推荐的方法 我应该使用 import 代替 所以我
  • 如何在expressjs中调用另一个api?

    我有一个这样的API app get test req res gt console log this is test 和另一个API app get check req res gt I want to call test api wit
  • 从 Puppeteer 中的 page.evaluate 获取元素? [复制]

    这个问题在这里已经有答案了 我正在与Node js and 傀儡师第一次 找不到输出值的方法page evaluate到外部范围 我的算法 Login Open URL Get ul 循环每个li然后点击它 等待innetHTML要设置并添
  • 使用 python-shell 持续交换数据

    我需要从节点运行一些 python 脚本 由于我的 python 脚本使用复杂的结构 我认为如果只加载这些结构一次 然后使用这些结构运行一些特定的脚本 任务 会更好 在节点上 我想永远运行一个脚本 或者直到我说它可以终止 并继续向该脚本发送
  • 创建shell可执行全局节点模块

    我尝试创建节点模块 我成功了 我用了npm install g在代码目录中 它创建了这个模块文件夹 AppData Roaming npm node modules myfirstmodule 现在我想让一个文件作为命令可执行 例如 pm2
  • 如何在前端和后端之间共享javascript代码(ES6)

    这是 ES6 特定的副本这个所以线程 https stackoverflow com questions 3225251 how can i share code between node js and the browser 其中详细介绍
  • 在 Phaser3 中从 Multiatlas 加载文件时出错

    尝试使用 Phaser 和 TexturePacker 中的多图集功能 出现此错误 VM32201 1 GET http localhost 8080 bg sd json 404 Not Found Texture js 250 Text
  • 为什么我在 tsx 文件中不断收到 The class method 'componentDidMount' Must be makred Either 'private' 'public' or 'protected' 警告?

    我不确定应该在反应类组件中标记我的方法 我在这些方法上收到此错误 componentDidMount componentDidUpdate componentWillUpdate 和 render 这是我拥有的一个基本组件 import a
  • 如何绕过警告意外的任何。指定不同的类型 @typescript-eslint/no-explicit-any

    我们有严格的零棉绒问题政策 这意味着所有错误和警告都需要修复 在我们的 React typescript 项目中面临这个 lint 错误 warning Unexpected any Specify a different type typ
  • ng2-select 中的数据绑定失败

    我正在尝试使用 ng2 select 将对象数组绑定到下拉列表 当我尝试使用字符串数组时它工作正常 private category Array value 1 text Table value 2 text Chair value 3 t
  • 如何取消/关闭 Angular 中的行为主体

    我有一个订阅了行为主题的组件ngOnInit生命周期 该组件 html 使用ngIf在渲染表格之前查看数据是否存在 当我离开该页面时 我想确保下次返回时 该表不可见 直到再次获取该数据 到目前为止 我能够做到这一点的唯一方法是调用该主题的
  • 如何在node.js中使用window.sessionstorage

    我使用 JavaScript 将哈希存储在会话存储中 如下所示 window sessionStorage setItem test true 如何使用 node js 读取此密钥 你不知道 会话存储 https developer moz
  • 在 ExpressJS 中通过管道传送远程文件

    我想读取远程图像并显示它 我可以保存文件 但无法正确显示代码 理想情况下 我只想直接传递文件而不进行处理 不确定是否需要 tmp 文件步骤 此代码不显示任何内容 没有错误 我也尝试了 res pipe response var url ht
  • NodeJS 内存增长 - (系统)内存泄漏?

    我在我们的生活环境中遇到了奇怪的内存泄漏 其中 system 堆中的对象不断增长 堆转储 Here is a memory dump where the memory usage grew to 800MB 请注意 该内存保留在Genera
  • 嵌套对象的 AJV 模式验证

    函数返回的对象看起来像这样 answer vehicle type 1 message Car model VW color red 答案 对象始终存在 其他字段基于 vehicle type E g 如果vehicle type 1 则有
  • Node.js 和 Express:异步操作后如何返回响应

    我是 Node js 新手 所以我仍然对异步函数和回调很感兴趣 我现在的难题是如何在异步操作中从文件读取数据后返回响应 我的理解是发送回复的工作方式如下 这对我有用 app get search function req res res s
  • 设计 Javascript 前端 <-> C++ 后端通信

    在我最近的将来 我将不得不制作一个具有 C 后端和 Web 前端的系统 要求 目前 我对此了解不多 我认为前端将触发数据传输 而不是后端 所以不需要类似 Comet 的东西 由于在该领域的经验可能很少 我非常感谢您对我所做的设计决策的评论

随机推荐