0

I'm using NestJS. I'm making a schema for my mongodb, the schema looks like this:

import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
import { ProductInCartDto } from "../dtos/product-in-cart.dto";

@Schema({ collection: 'carts', timestamps: true })
export class Cart {
    @Prop()
    userId: number;

    @Prop()
    products: ProductInCartDto[];

}

export const CartSchema = SchemaFactory.createForClass(Cart);

The ProductInCartDto looks like this:

export class ProductInCartDto {

    storeId: number;

    productList: {[productId: number]: number}[];
    
}

This is the model I used to test the schema:

import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Cart } from '../domains/schemas/cart.schema';
import { Model } from 'mongoose';

@Injectable()
export class CartModel {
    constructor(@InjectModel(Cart.name) private cartModel: Model<Cart>) {}
    
    async createCart() {
        return await this.cartModel.create({ 
            userId: 1,
            products: [{
                storeId: 1,
                productList: {
                    1: 1,
                    2: 2
                }
            }]
        });
    }
}

When I execute the model, it return the following error:

[Nest] 33570  - 06/24/2024, 9:14:36 PM   ERROR [HttpExceptionFilter] Maximum call stack size exceeded
RangeError: Maximum call stack size exceeded
    at MetadataStorage.getExposedProperties (/home/long/vegeta/HealthyFoodApplication_BackEnd/node_modules/src/MetadataStorage.ts:115:23)
    at TransformOperationExecutor.getKeys (/home/long/vegeta/HealthyFoodApplication_BackEnd/node_modules/src/TransformOperationExecutor.ts:461:54)
    at TransformOperationExecutor.transform (/home/long/vegeta/HealthyFoodApplication_BackEnd/node_modules/src/TransformOperationExecutor.ts:150:25)
    at TransformOperationExecutor.transform (/home/long/vegeta/HealthyFoodApplication_BackEnd/node_modules/src/TransformOperationExecutor.ts:327:31)
    at TransformOperationExecutor.transform (/home/long/vegeta/HealthyFoodApplication_BackEnd/node_modules/src/TransformOperationExecutor.ts:32

However, the data record is still added to the mongodb database regardless of the error. Also I figured out that this error only occur when there is an property of array in the schema (ProductInCartDto[] in this case). If I don't use array, the error will disappear

What can be the cause here?

I tried changing data structure of ProductInCartDto but it doesn't work. I can't remove the array of ProductInCartDto[] since this property need to be an array

1 Answer 1

0

according to Maximum call stack size exceeded error, your DTO is not a standard DTO which is nested function, beside that, its not good way to defined Maps in mongoose

if you want to do this:
  {
     userId: 1,
     products: [{
            storeId: 1,
            productList: {
                 1: 1,
                 2: 2
                }
           }]
   }

you must FIRST define Product:

@Schema()
export class Product {
    @Prop({ required: true })
    storeId: number;

    @Prop({ type: Map, of: Number })
    productList: Map<number, number>;
}

Then Carts

@Schema({ collection: 'carts', timestamps: true })
export class Cart {
    @Prop({ required: true })
    userId: number;

    @Prop({ type: [ProductSchema], default: [] })
    products: Product[];
}

insert a Mock Data

  return await this.cartModel.create({
            userId: 1,
            products: [
                {
                    storeId: 1,
                    productList: new Map([
                        [1, 1],
                        [2, 2],
                    ]),
                },
            ],
        });
Sign up to request clarification or add additional context in comments.

2 Comments

I tried your solution but new error arised: ERROR [HttpExceptionFilter] Cart validation failed: products.0.productList: Cast to Map failed for value "Map(2) { 1 => 1, 2 => 2 }" (type Map) at path "productList" because of "TypeError" As far as I know, this is due to mongoose being unable to cast the 'Map' object
i show you the right way, you must solve the becoming problems...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.