#ReferenceError: Cannot access 'ProductDetailsModel' before initialization

13 messages · Page 1 of 1 (latest)

young panther
#

and below is /admin/products/models/return-policy.model.ts:

import { Column, Entity, OneToOne, PrimaryGeneratedColumn } from "typeorm";

// Product Model
import { ProductDetailsModel } from "./product.model";

@Entity({ name: "returnPolicy" })
export class ReturnPolicy {
  @PrimaryGeneratedColumn("uuid")
  returnPolicyId: string;

  @Column({ type: "int", nullable: false })
  returnDuration: number; // Number of days allowed for return (e.g., 7)

  @Column({ type: "varchar", length: 255, nullable: false })
  returnWindow: string; // e.g., "7 days", "30 days"

  @Column({ type: "text", array: true, nullable: false })
  conditions: Array<string>; // Conditions for return (e.g., unworn, tags attached)

  @Column({ type: "text", nullable: false })
  policyInformation: string; // Detailed description of the return policy

  @OneToOne(() => ProductDetailsModel, productTable => productTable.returnPolicyId, { onDelete: "CASCADE" })
  productDetailId: ProductDetailsModel; // Link to the product this policy belongs to

  @Column({ default: new Date() })
  createdAt: string;
}
#

and I've added both Models in the products.module.ts as below:

@Module({
  imports: [TypeOrmModule.forFeature([CareInstruction, ProductSize, ProductDetailsModel, ReturnPolicy])],
  controllers: [ProductsController],
  providers: [ProductsService],
})
#

when I run npm run start, then I am getting below error:

export class ProductDetailsModel {
             ^


ReferenceError: Cannot access 'ProductDetailsModel' before initialization
    at ProductDetailsModel (C:\Users\dhaval\Desktop\Coursera Learning\Free_Projects\fabric-fusion\server\src\admin\products\models\product.model.ts:13:14)
    at Object.<anonymous> (C:\Users\dhaval\Desktop\Coursera Learning\Free_Projects\fabric-fusion\server\src\admin\products\models\return-policy.model.ts:23:97)
    at Module._compile (node:internal/modules/cjs/loader:1469:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1548:10)
    at Module.load (node:internal/modules/cjs/loader:1288:32)
    at Module._load (node:internal/modules/cjs/loader:1104:12)
    at Module.require (node:internal/modules/cjs/loader:1311:19)
    at require (node:internal/modules/helpers:179:18)
    at Object.<anonymous> (C:\Users\dhaval\Desktop\Coursera Learning\Free_Projects\fabric-fusion\server\src\admin\products\models\product.model.ts:5:29)
    at Module._compile (node:internal/modules/cjs/loader:1469:14)

Node.js v20.17.0

any help or idea, why I am getting this error?

undone siren
#

Most likely because of cyclic file imports

young panther
young panther
#

@undone siren -- I've fixed the above issue by using Relation<CareInstruction>, now it's working fine, and it's resolved.

#

@undone siren --- I need help with other issue as:

@Entity({ name: "productSubCategory" })
export class ProductSubCategory {
  @PrimaryGeneratedColumn("uuid")
  productSubCategoryId: string;

  @Column({ type: "varchar", length: 255, unique: true, nullable: false })
  productSubCategoryName: string;

  @Column({ type: "text", nullable: false })
  productSubCategoryImage: string;

  @ManyToOne(() => ProductCategory, productCategory => productCategory.productCategoryId, { onDelete: "CASCADE" })
  @JoinColumn({ name: "productCategoryFk" })
  productCategoryFk: ProductCategory;

  @OneToMany(() => ProductDetailsModel, productTable => productTable.productSubCategoryFk)
  productDetailsFk: ProductDetailsModel[];

  @Column({ default: false })
  isDeleted: boolean;

  @Column({ default: new Date() })
  createdAt: string;
}

now in this ProductSubCategory table, I am trying insertion query and below is the Code Snippet:

async create(id: string, createSubCategoryDto: CreateSubCategoryDto): Promise<ProductSubCategory> {
    const productSubCateg = new ProductSubCategory();

    productSubCateg.productSubCategoryName = createSubCategoryDto.productSubCategoryName;
    productSubCateg.productSubCategoryImage = createSubCategoryDto.productSubCategoryImage;
    productSubCateg.productCategoryFk = id;

    return this.subCategRepository.save(productSubCateg);
  }

for this productSubCateg.productCategoryFk = id; I am getting below error:

Type 'string' is not assignable to type 'ProductCategory'.ts(2322)
(property) ProductSubCategory.productCategoryFk: ProductCategory

and I got one solution for this, but my other issue is occuring is that, this return this.subCategRepository.save(productSubCateg) is returning entire productCategoryFk Object which contains Category details

#

@undone siren any idea how to fix it?

young panther
#
{
    "productSubCategoryName": "Jeans",
    "productSubCategoryImage": "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRHuyzWYfdN2lRIQo3o04wHn4-3hoz7gAuG_Q&s",
    "productCategoryFk": {
        "productCategoryId": "4e15f66d-c413-40e3-97d5-40e82ad3ef8b",
        "productCategoryName": "Western Wear",
        "isDeleted": false,
        "createdAt": "\"2024-10-10T07:53:35.727Z\""
    },
    "productSubCategoryId": "ea7ebb15-019c-41ce-9baf-99cc06d2a8dd",
    "isDeleted": false,
    "createdAt": "\"2024-10-12T02:10:18.757Z\""
}

this something I am getting when I am doing that .save() method, but I want productCategoryFk to return like below only:

"productCategoryFk": "4e15f66d-c413-40e3-97d5-40e82ad3ef8b",

@undone siren -- how to achieve this?

restive stirrupBOT
#

This post has been marked as resolved. :white_check_mark:
Please read through the conversation and resolution, if you are having the same issue. If you were the original author of the post and the issue is still fresh (within a few days) and you are still have having trouble, continue to reply here. If you are not the original author of the post or the post has aged, start a new thread linking this one as relevant to your problem, providing as much additional information as possible.

red quarry
red quarry
#

ORM methods such as .save() return the entity "as it is".
If you want to return something else, you have either to permorm another DB request, or to tranform the entity.
Automapper is helpul to do such transformations :
https://www.npmjs.com/package/@automapper/core