Skip to content

Virtuals not serialized after v8.5 #14771

Closed

Description

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.5.2

Node.js version

v22.5.1

MongoDB server version

7.0.12

Typescript version (if applicable)

5.5.4

Description

I have a nested schema, that contains calculated (virtual) props based on level. After updating from 8.4.5 to 8.5.2, the virtuals are no longer serialized. I'm using Mongoose with @nestjs/mongoose if that matters, it hasn't been updated since.

enum EProduct {
  TOYS = "Toys",
}

const labs: Record<EProduct, Lab> = {
  [EProduct.TOYS]: {
    baseCapacity: 100,
    baseProduction: 10,
  },
}


@Schema({ _id: false })
export class UserLab {
  @Prop({ required: true })
  product: EProduct;

  @Prop({ required: true, default: 1 })
  capacityLevel: number;

  @Prop({ required: true, default: 1 })
  productionLevel: number;

  @Prop({ required: true, default: new Date() })
  collectTime: Date;

  @Prop({
    virtual: true,
    get: function () {
      const lab = labs[this.product];
      return this.capacityLevel * lab.baseCapacity;
    },
  })
  capacity?: number;

  @Prop({
    virtual: true,
    get: function () {
      const lab = labs[this.product];
      return this.productionLevel * lab.baseProduction;
    },
  })
  production?: number;

  @Prop({
    virtual: true,
    get: function () {
      const now = new Date();
      const diff = getUnixTime(now) - getUnixTime(this.collectTime);
      const productionPerSecond = this.production / 3600;
      const produced = Math.floor(productionPerSecond * diff);
      return produced < this.capacity ? produced : this.capacity;
    },
  })
  produced?: number;
}

@Schema({ _id: false })
export class LabPlot {
  @Prop({ required: true })
  plotId: number;

  @Prop({ type: UserLab, required: false })
  lab?: UserLab;
}

@Schema({
  toObject: {
    getters: true,
  },
  toJSON: {
    getters: true,
  },
})
export class User extends Document {
  @Prop({ required: true, unique: true })
  id: number;

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

  @Prop({ type: [LabPlot], default: [{ plotId: 0 }] })
  labPlots: LabPlot[];
}

v8.4.5

{
    "_id": "66a9f217839a26bca6511464",
    "id": 475680949,
    "username": "alko89",
    "labPlots": [
        {
            "plotId": 0,
            "lab": {
                "product": "Toys",
                "capacityLevel": 1,
                "productionLevel": 1,
                "collectTime": "2024-07-31T08:14:37.829Z",
                "produced": 65,
                "production": 10,
                "capacity": 100
            }
        }
    ],
    "__v": 0
}

v8.5.2

{
    "_id": "66a9f217839a26bca6511464",
    "id": 475680949,
    "username": "alko89",
    "labPlots": [
        {
            "plotId": 0,
            "lab": {
                "product": "Toys",
                "capacityLevel": 1,
                "productionLevel": 1,
                "collectTime": "2024-07-31T08:14:37.829Z"
            }
        }
    ],
    "__v": 0
}

Steps to Reproduce

Update to v8.5.2

Expected Behavior

Virtuals should be serialized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions