Accessing properties of embedded discriminator in array throws error

In my NestJS app, in one of my Mongoose schemas I have embedded discriminators in an array. When iterating over the array and trying to access fields specific to one discriminator, for some reason this field is undefined.

Here for reference the NestJS docs on how to handle discriminators.

Schema definitions

User

@Schema()
export class User {
  @Prop({ required: true, type: [BaseAction] })
  subscriptionHistory: BaseAction[];
}

export type UserDocument = HydratedDocument<User>;
export const UserSchema = SchemaFactory.createForClass(User);
export type UserModel = Model<UserDocument>;

BaseAction and discriminators

@Schema({ _id: false, discriminatorKey: 'action', autoCreate: false })
export class BaseAction {
  @Prop({ required: true, type: String, enum: SubscriptionAction })
  action: string;

  @Prop({ default: Date.now })
  timestamp: Date;
}
export const BaseActionSchema = SchemaFactory.createForClass(BaseAction);


@Schema({ _id: false })
export class UpgradeAction {
  action: string = SubscriptionAction.Upgrade;
  timestamp: Date;

  @Prop({ required: true, type: Object })
  details: SubscriptionChange & StartDateChange & EndDateChange;
}
export const UpgradeActionSchema = SchemaFactory.createForClass(UpgradeAction);

/*
 * Here exemplary the SubscriptionChange helper used in the 'details' prop
 */
@Schema({ _id: false })
class SubscriptionChange {
  @Prop({ required: true })
  oldVersion: number;

  /* Some more properties */
}

Module

@Module({
  imports: [
    MongooseModule.forFeature([
      { name: User.name, schema: UserSchema },
      {
        name: BaseAction.name,
        schema: UserSchema.path('subscriptionHistory').schema,
        discriminators: [
          { name: SubscriptionAction.Upgrade, schema: UpgradeActionSchema },
          /* further discriminators here */
        ],
      },
    ])
  ],
  /* controllers, providers, exports */
})
export class UserModule {}

The issue

In one of my services, I retrieve the user from the database and iterate over the subscriptionHistory array. I check for the action type and then want to read the details property of the action.

When running in debug, I can see that the details field is in memory, but whenever I access it, it is undefined.

export class SomeService {
  async someHandler(userId) {
    const user = await this.userService.getUserById(userId); // works fine

    const action = user.subscriptionHistory.find((h) => {
      if (h.action === SubscriptionAction.Upgrade) { // works fine
        console.log(h) // <-- 'details' is being printed
        const { details } = h as UpgradeAction // I have to cast to be able to extract details
        return details.oldVersion > 1; // This throws
      }
    });
  }
}

The error being thrown is:

Cannot read properties of undefined (reading ‘oldVersion’)

In the debugger, I can see that h is of type EmbeddedDocument and that details is there, but not accessible.

enter image description here

I believe, the issue has something to do with me not type casting correctly. Unfortunately, I don’t know how to solve this. How can I access details?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật