【发布时间】:2021-09-08 16:08:45
【问题描述】:
我正在尝试在 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';
});```
【问题讨论】:
标签: mongodb mongoose graphql mongodb-query nestjs