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