Mongoose populate is breaking my Serverless application when upgrading to Node JS 20X

I have a REST API that I’m migrating from Node16 to Node20 in Serverless.
Everything works fine except the populate method for my queries.
Im using:
[email protected]
[email protected]
node v20.15.1

This is my DealershipSchema file

import { Schema } from 'mongoose';
import { DMSType } from '../../common/activeDMS';
import { DmsModel } from './dms';

export interface DealershipModel {
    _id?: string;
    name: string;
    timezone: string;
    organizationId: string;
    dms: DMSType;
    dmsData: DmsModel;
    url?: string;
    estimateUrl?: string;
    warehouseId?: string;
    repairShopId?: string;
    any_data?: any;
    authData?: any;
    productCredentials?: any;
    workshopId?: string;
}

const DealershipSchema = new Schema({
    name: {
        type: String,
        required: true,
    },
    timezone: {
        type: String,
        required: true,
    },
    dms: {
        type: String,
        required: true,
    },
    dmsData: { type: Schema.Types.ObjectId, ref: 'Dms', required: true },
    organizationId: {
        type: String,
        required: true,
    },
    url: {
        type: String,
        required: false,
    },
    estimateUrl: {
        type: String,
        required: false,
    },
    repairShopId: {
        type: String,
        required: false,
    },
    workshopId: {
        type: String,
        required: false,
    },
    warehouseId: {
        type: String,
        required: false,
    },
    any_data: {
        type: Object,
        required: false,
    },
    authData: {
        type: Object,
        required: false,
    },
    productCredentials: {
        type: Object,
        required: false,
    },
});

export default DealershipSchema;

This is the DmsSchema file:

import { Schema } from 'mongoose';

export interface DmsModel {
    _id?: string;
    name: string;
    authorization: string;
    url: string;
    estimateUrl?: string;
    environment: string;
    authEndpoint?: string;
    authData?: any;
    authHost?: string;
    dealerAuthData?: any;
}

const DmsSchema = new Schema({
    name: String,
    authorization: String,
    url: String,
    estimateUrl: String,
    environment: String,
    authEndpoint: String,
    authHost: { type: String, required: false },
    authData: { type: Object, required: false },
    dealerAuthData: { type: Object, required: false },
});

export default DmsSchema;

Here is where I’m creating all models and connections in my config file:

private async createConnectionsAndModels() {
        try {
            this.oaDB = await mongoose.connect(this.oaMongoConnection);

            this.connected = true;
            const DmsModel = model('Dms', DmsSchema);
            const DealershipModel = model('Dealership', DealershipSchema);
            const UserModel = model('User', UserSchema);
            const CacheModel = model('Cache', CacheSchema);
            const EstablishmentModel = model('Establishment', EstablishmentSchema);
            const DashboardCredentialModel = model(
                'DashboardCredential',
                dashboardCredentialSchema
            );
            const ApplicationModel = model('Application', ApplicationSchema);
            const OrganizationModel = model('Organization', OrganizationSchema);
            this.models = {
                DealershipModel,
                UserModel,
                CacheModel,
                EstablishmentModel,
                DmsModel,
                DashboardCredentialModel,
                ApplicationModel,
                OrganizationModel,
            };
            return this.models;
        } catch (e) {
            console.log(e);
            this.connected = false;
            throw new Error('Error al conectar con la bd');
        }
    }

and this is where it fails:

public async getDealershipById(dealershipId: string): Promise<ResponseAws> {
   const models = await mongoDB.getModels();
   const DealershipSchema = models.DealershipModel;
   let dealershipData = await DealershipSchema.findById(dealershipId).populate('dmsData').exec();

If I don’t populate the field it works fine, no problem at all, but as soon as I try to populate the ‘dmsData’ field it completely breaks the app and shows this error:

× Uncaught exception
Environment: win32, node 20.15.1, framework 3.32.2 (local), plugin 6.4.0, SDK 4.5.1
Docs:        docs.serverless.com
Support:     forum.serverless.com
Bugs:        github.com/serverless/serverless/issues

Error:
Error [ERR_UNHANDLED_ERROR]: Unhandled error. ({})
    at Worker.emit (node:events:508:17)
    at Worker.emit (node:domain:488:12)
    at [kOnErrorMessage] (node:internal/worker:326:10)
    at [kOnMessage] (node:internal/worker:337:37) 
    at MessagePort.<anonymous> (node:internal/worker:232:57)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:820:20)
    at MessagePort.<anonymous> (node:internal/per_context/messageport:23:28)

It works if I first get the dealership and then “manually” find the dms and append it, like this:

let dms = await DmsSchema.findById(dealershipData.dmsData).exec();
dealershipData.dmsData = dms;

however that is definitelly not what I want to do.

New contributor

Mario Carrillo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

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