Facing an error while trying to save to postgres database

Viewed 169

I'm trying to save data in nest api using TypeORM + PostgreSQL. When saving saving my entity to the entity's repository, I receive the error:

TypeError: relatedEntities.forEach is not a function and the entity is not saved to the database.

This seems to only happen when I am using the @OneToMany or @TreeChildren decorators.

TypeError: relatedEntities.forEach is not a function\n    at OneToManySubjectBuilder.buildForSubjectRelation (C:\\projects\\nestjs\\geep-account-service\\src\\persistence\\subject-builder\\OneToManySubjectBuilder.ts:85:25)\n    at C:\\projects\\nestjs\\geep-account-service\\src\\persistence\\subject-builder\\OneToManySubjectBuilder.ts:42:22\n    at Array.forEach (<anonymous>)\n    at C:\\projects\\nestjs\\geep-account-service\\src\\persistence\\subject-builder\\OneToManySubjectBuilder.ts:36:49\n    at Array.forEach (<anonymous>)\n    at OneToManySubjectBuilder.build (C:\\projects\\nestjs\\geep-account-service\\src\\persistence\\subject-builder\\OneToManySubjectBuilder.ts:35:23)\n    at EntityPersistExecutor.<anonymous> (C:\\projects\\nestjs\\geep-account-service\\src\\persistence\\EntityPersistExecutor.ts:107:59)\n    at step (C:\\projects\\nestjs\\geep-account-service\\node_modules\\typeorm\\node_modules\\tslib\\tslib.js:143:27)\n    at Object.next (C:\\projects\\nestjs\\geep-account-service\\node_modules\\typeorm\\node_modules\\tslib\\tslib.js:124:57)\n    at fulfilled (C:\\projects\\nestjs\\geep-account-service\\node_modules\\typeorm\\node_modules\\tslib\\tslib.js:114:62)\n    at processTicksAndRejections (node:internal/process/task_queues:96:5)

here is the loan schema

import { TimeStamp } from '../../states/entities/timestamp.entity';
import {
  Column,
  Entity,
  JoinColumn,
  ManyToOne,
  OneToMany,
  PrimaryGeneratedColumn,
} from 'typeorm';
import { Individual } from '../../individuals/entities/individual.entity';
import { LoanStructure } from '../../loan-structure/entities/loan-structure.entity';
import { LoanApplication } from '../enums/loan-application.enum';
import { LoanRepaymentHistory } from '../../loan-repayment/entities/loan_repayment_history.entity';
import { TransactionHistory } from '../../transaction-history/entities/transaction-history.entity';

@Entity('loans')
export class Loan extends TimeStamp {
  @PrimaryGeneratedColumn()
  id: number;

  @ManyToOne(() => LoanStructure, (loanStructure) => loanStructure.loans, {
    eager: true,
  })
  loanStructure: LoanStructure;

  @ManyToOne(() => Individual, (individual) => individual.loan)
  applicant: Individual;

  @Column({ type: 'varchar', nullable: true, name: 'loan_amount' })
  loanAmount: number;

  @OneToMany(
    () => LoanRepaymentHistory,
    (loanRepaymentHistory) => loanRepaymentHistory.loan,
    {
      eager: true,
    },
  )
  LoanRepaymentHistory: LoanRepaymentHistory[];

  @OneToMany(
    () => TransactionHistory,
    (transactionHistory) => transactionHistory.loan,
    {
      eager: true,
    },
  )
  @JoinColumn({ name: 'transaction_history' })
  transactionHistory: TransactionHistory[];

  @Column({
    type: 'enum',
    enum: LoanApplication,
    nullable: true,
    default: LoanApplication.Pending,
    name: 'loan_approval',
  })
  loanApproval: LoanApplication;

  @Column({ type: 'date', nullable: true, name: 'loan_approval_date' })
  loanApprovalDate: Date;

  @Column({
    type: 'enum',
    enum: LoanApplication,
    default: LoanApplication.Pending,
    nullable: true,
    name: 'loan_disbursement',
  })
  loanDisbursement: LoanApplication;

  @Column({ type: 'date', nullable: true, name: 'loan_disbursement_date' })
  loanDisbursementDate: Date;

  @Column({
    type: 'enum',
    enum: LoanApplication,
    default: LoanApplication.Pending,
    nullable: true,
    name: 'loan_payment_completed',
  })
  loanPaymentCompleted: LoanApplication;

  @Column({
    type: 'date',
    nullable: true,
    name: 'loan_payment_completed_date',
  })
  loanPaymentCompletedDate: Date;
}

here is loan_structure schema

import { TimeStamp } from '../../states/entities/timestamp.entity';
import {
  Column,
  Entity,
  OneToMany,
  OneToOne,
  PrimaryGeneratedColumn,
} from 'typeorm';
import { Programme } from '../../programmes/entities/programme.entity';
import { Loan } from '../../loans/entities/loan.entity';
import { PaymentInterval } from '../enums/loan-payment-interval.enum';

@Entity('loan_structure')
export class LoanStructure extends TimeStamp {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ type: 'int', nullable: false, name: 'loan_period' })
  loanPeriod: number;

  @Column({
    type: 'enum',
    enum: PaymentInterval,
    default: PaymentInterval.Monthly,
    nullable: true,
    name: 'payment_interval',
  })
  paymentInterval: PaymentInterval;

  @Column({ type: 'text', nullable: true, name: 'loan_description' })
  loanDescription: string;

  @Column({ type: 'varchar', default: 0, name: 'repayment_amount' })
  repaymentAmount: number;

  @OneToOne(() => Programme, (programme) => programme.loanStructure)
  programme: Programme;

  @OneToMany(() => Loan, (loan) => loan.loanStructure)
  loans: Loan[]; //causing the error
}
0 Answers
Related