node.js - NestJS Mongoose 扩展架构并覆盖父级的属性

标签 node.js typescript mongodb mongoose nestjs

我希望我的B 类能够扩展A 类。这按预期工作,但现在我需要重写 B 类中 A 类的属性。

具体来说,我必须将 A 类的属性设置为 B 类的可选属性:

export class B extends A {
  // This property is available in Class A
  @Prop({ required: false, index: true })
  @ApiProperty()
  propertyToOverride: number;
}

最佳答案

我在 NestJS 的 Discord 中询问了这个问题,这是我从版主 Scott 那里得到的答案(Scott (EN-DE)#0052):

There is no overriding of prop properties. And, the classic inheritance of schema definitions doesn't work either, i.e. with overriding. If you are extending from a schema class, you can only add additional fields to it in your new class. So, it is a reusability tool and not a polymorphism tool. If you need polymorphism, then you need to look at discriminators.

不幸的是,目前似乎没有办法通过继承来做到这一点:(

关于node.js - NestJS Mongoose 扩展架构并覆盖父级的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71172139/

相关文章:

javascript - 插入行时解析错误

reactjs - 使用异步 redux-thunk 操作进行存储更改 typescript 的功能测试

javascript - 如何使用 Apollo 在 React 中执行 GraphQL 查询 onClick?

node.js - 我如何更新 MongoDB 中没有 id 的数组中的所有对象

node.js - MongoDb中基于日期的数据清除逻辑

node.js - 使用 Zombie 测试 Node.js 应用程序

javascript - 预期数组但收到...(JSON)... AngularJS 错误

javascript - OpenAI API错误: "Module ' openai' has no exported member 'Configuration' "

javascript - 将枚举从 Typescript 转换为 JavaScript 时出错

mongodb - 如何在mongodb中返回弹出的元素