mongodb - Nestjs - Mongoose - 虚拟字段 - 无法在 graphql 操场上查询
问题描述
我正在尝试在nestjs-mongoose 中创建一个虚拟字段。该代码没有显示任何错误,但我无法在 graphql 游乐场中查询该虚拟字段。
我厌倦了使用 schema.virtual("virtualFieldName") 添加虚拟字段。然后我为各自的模式启用了 {virtuals: true} 。但仍然无法查询该字段。
我是否需要在 DTO 文件中添加任何内容?
Entity.ts / 架构文件
@Schema({
collection: 'v3_customer_account_selectors',
timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' },
toJSON: { virtuals: true },
})
@Injectable({ scope: Scope.TRANSIENT })
export class AccountSelectorEntity extends BaseEntity {
@AliasableProp({ alias: '_type', type: AccountSelectorType })
type: AccountSelectorType;
@Prop()
name?: string;
@AliasableProp({ alias: 'last_checked', type: Date })
lastChecked?: Date;
@AliasableProp({
type: SchemaTypes.ObjectId,
alias: 'customer_id',
})
customerId?: Types.ObjectId;
}
export const AccountSelectorEntitySchema = SchemaFactory.createForClass(
AccountSelectorEntity
);
AccountSelectorEntitySchema.set('toJSON', { getters: true, virtual: true });
AccountSelectorEntitySchema.set('toObject', { getters: true, virtual: true });
AccountSelectorEntitySchema.virtual('selector_type').get(function () {
return 'TestReturn';
});```
解决方案
您必须创建一个类型或另一个类,而不是使用实体。实体虚拟不在实体文档类中。
推荐阅读
- r - 显示方位和距离的圆图
- r - 如何折叠具有重复 ID 和每个 ID 不同缺失值的数据框,以便将 NA 替换为重复 ID 中的值?(在 R 中)
- c++ - 在一个语句中比较布尔变量,只要数组 C++
- eclipse - 将自然语言转换为 OCL 脚本
- javascript - 在 Switch Case PHP 中使用具有不同值的相同代码 Jquery 时出现问题
- python - 使用 numpy 解决一个未知的线性系统?
- haskell - Haskell:如何生成所有可能的元素组合,列表中的每个元素,以及任意数量的列表
- dart - Flutter:连接微软Outlook Exchange服务器
- c# - 错误:此方法需要“字段”参数
- python - 使用 Python 和 NLP,如何从具有大量自由文本的 Excel 列中提取某些文本字符串和字符串前面的相应数字?