Mario Carrillo
Mario Carrillo

Reputation: 1

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: mongoose@7.7.0 serverless@3.32.2 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.

Upvotes: 0

Views: 26

Answers (0)

Related Questions