首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用NestJS注入ObjectionJS模型会抛出异常

使用NestJS注入ObjectionJS模型会抛出异常
EN

Stack Overflow用户
提问于 2021-11-18 17:58:35
回答 1查看 107关注 0票数 0

当我尝试将Objection.js模型注入NestJs服务时:

constructor(@Inject('UserModel') private readonly modelClass: ModelClass<UserModel>) {}

我在编译时得到一个Maximum call stack size exceeded错误。我认为这是由于我的其他模型和一些循环依赖。我删除了一个单独的模型之外的所有东西,仍然得到了异常。

如果我在tsconfig.json中设置了"strict": true,代码就会按预期构建和运行。我该如何补救这种情况?

下面是使用的模型和包。

基本模型

代码语言:javascript
复制
export default class BaseModel extends Model {
  readonly id: number;
  createdBy: number;
  created!: string;
  modified: string;
  modifiedBy: number; 
}

用户模型

代码语言:javascript
复制
export default class UserModel extends BaseModel {
  static tableName = 'users';
  static virtualAttributes = ['fullName'];

  firstName!: string;
  lastName!: string;
  company?: string;
  email!: string;

  fullName(): string {
    return `${this.firstName} ${this.lastName}`;
  }


  static jsonSchema = {
    type: 'object',
    required: ['firstName', 'lastName', 'email'],
    properties: {
      id: { type: 'number' },
      firstName: { type: 'string' },
      lastName: { type: 'string' },
      company: { type: 'string' },
      email: { type: 'string' },
    },
  };
}

包:

代码语言:javascript
复制
  "dependencies": {
    "@apollo/gateway": "^0.44.0",
    "@nestjs/common": "^8.2.1",
    "@nestjs/core": "^8.2.1",
    "@nestjs/graphql": "^9.1.1",
    "@nestjs/platform-express": "^8.0.0",
    "ajv-formats": "^2.1.1",
    "apollo-server-express": "^3.5.0",
    "class-transformer": "^0.4.0",
    "class-validator": "^0.13.1",
    "dayjs": "^1.10.7",
    "dotenv": "^10.0.0",
    "graphql-subscriptions": "^2.0.0",
    "knex": "^0.95.14",
    "objection": "^3.0.0",
    "pg": "^8.7.1",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^7.4.0"
  },
  "devDependencies": {
    "@nestjs/cli": "^8.0.0",
    "@nestjs/schematics": "^8.0.0",
    "@nestjs/testing": "^8.0.0",
    "@types/express": "^4.17.13",
    "@types/jest": "^27.0.1",
    "@types/node": "^16.0.0",
    "@types/supertest": "^2.0.11",
    "@typescript-eslint/eslint-plugin": "^5.0.0",
    "@typescript-eslint/parser": "^5.0.0",
    "eslint": "^8.0.1",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-import": "2.25.2",
    "eslint-plugin-prettier": "^4.0.0",
    "jest": "^27.2.5",
    "prettier": "^2.3.2",
    "source-map-support": "^0.5.20",
    "supertest": "^6.1.3",
    "ts-jest": "^27.0.3",
    "ts-loader": "^9.2.3",
    "ts-morph": "^12.2.0",
    "ts-node": "^10.0.0",
    "tsconfig-paths": "^3.10.1",
    "typescript": "^4.4.2"
  },
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-18 19:01:54

必须在strict模式下运行According to this issue Typescript才能正确转换代码。看起来Objection v3还处于某种预发布状态,所以还没有很好的文档记录。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70024609

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档