响应中无法返回 mongoose 虚拟字段

问题描述 投票:0回答:1

我有以下带有几个虚拟字段的模式。我无法在响应中恢复这些虚拟:

import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';

import mongoose from 'mongoose';
import { Sportclub } from './sportclub.schema';
export type SportEventDocument = SportEvent & mongoose.Document;

@Schema({ timestamps: true, toJSON: { virtuals: true } })
export class SportEvent {
  @Prop({ required: true })
  title: string;

  @Prop({ required: true })
  description: string;

  @Prop({
    required: true,
    min: 0,
    validate: {
      validator: (value: number) => {
        // Check if the value is a positive number
        return value >= 0;
      },
      message: 'Price must be a positive number',
    },
  })
  price: number;

  @Prop({ required: true })
  startDateAndTime: Date;

  @Prop({
    required: true,
    min: 0,
    validate: {
      validator: (value: number) => {
        // Check if the value is a positive number
        return value >= 0;
      },
      message: 'Duration must be a positive number',
    },
  })
  durationInMinutes: number;

  @Prop({
    required: true,
    min: 1,
    validate: {
      validator: (value: number) => {
        // Check if the value is a positive number greater than or equal to 1
        return value >= 1;
      },
      message:
        'Maximum number of participants must be a positive number greater than or equal to 1',
    },
  })
  maximumNumberOfParticipants: number;

  @Prop({
    required: false,
    default: [],
    type: [mongoose.Schema.Types.ObjectId],
    ref: 'User',
  })
  enrolledParticipants: mongoose.Schema.Types.ObjectId[];

  @Prop({ required: true })
  hostId: mongoose.Schema.Types.ObjectId;

  @Prop({ required: true })
  sportclub: Sportclub;
}

export const SportEventSchema = SchemaFactory.createForClass(SportEvent);

//virtuals
SportEventSchema.virtual('enrolledParticipantsCount').get(function () {
  return this.enrolledParticipants.length;
});

SportEventSchema.virtual('maximumIncome').get(function () {
  return this.maximumNumberOfParticipants * this.price;
});

SportEventSchema.virtual('currentIncome').get(function () {
  return this.enrolledParticipants.length * this.price;
});

SportEventSchema.virtual('isFull').get(function () {
  return this.enrolledParticipants.length >= this.maximumNumberOfParticipants;
});

我尝试使用此方法在 api 调用中检索所有体育赛事:

  // get all sport events.
  async getAllSportEvents(): Promise<SportEvent[]> {
    console.log('get all sportevents service (api) called');
    try {
      const result: SportEvent[] = await this.sportEventModel
        .find()
        .lean({ virtuals: true });
      console.log(result);
      return result;
    } catch (error) {
      throw new HttpException(error.message, 400);
    }
  }

这是 SportEvent 的界面:

import { Sportclub } from './Sportclub';

export interface SportEvent {
  _id?: string;
  title: string;
  description: string;
  price: number;
  startDateAndTime: Date;
  durationInMinutes: number;
  maximumNumberOfParticipants: number;
  enrolledParticipants?: string[];
  hostId: string;
  sportclub: Sportclub;
  //virtuals
  enrolledParticipantsCount?: number;
  isFull?: boolean;
  maximumIncome?: number;
  currentIncome?: number;
}

响应不包含虚拟字段。我不知道为什么没有。有谁知道吗?

typescript mongodb mongoose nosql
1个回答
0
投票

发现问题,这部分:

.lean({ virtuals: true });
不知何故仍然删除了虚拟。我刚刚删除了这一行,现在它可以工作了。如果有人知道为什么,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.