Tuesday, 28 February 2023

I am struggling to save data into postgreSql table using typeORM and Nestjs with one to many relationship

I have 2 entities i.e Customer, Orders with one to many relationship. A customer can have multiple orders and an order can be owned by only one customer. I am creating an api to create order that is a POST api.

order.controller.ts

@Post()
async createOrder(@Body() order: CreateOrderDto) {
  const newOrder = await this.orderService.createOrder(order);
  return newOrder;
}

order.service.ts

async createOrder(order: IOrder) {
  const newOrder = this.orderRepository.create(order);
  await this.orderRepository.save(newOrder);
  return newOrder;
}

create-order.dto.ts

export class CreateOrderDto {
 @IsString()
 @IsNotEmpty()
 @Length(5, 400)
 public description: string;

 @IsNotEmpty()
 @IsNumber()
 @IsPositive()
 public amount: number;

 @IsNotEmpty()
 @IsString()
 public paymentMethod: string;

 @IsOptional()
 @IsString()
 public customizeName: string;

 @IsOptional()
 @IsString()
 public weight: string;

 @IsNotEmpty()
 public customer: ICustomer;

 @IsNotEmpty()
 @IsArray()
 public products: Array<IProduct>;
}

I am sending this json from postman

{
   "description": "Customized waullet",
   "quantity": 1,
   "amount": 1500,
   "paymentMethod": "cashOnDelivery",
   "customizeName": "Faizan",
   "weight": "2.5g",
   "customer": {
       "fullName": "Anas Subzwari",
       "age": 20,
       "country": "Pakistan",
       "city": "Karachi",
       "address": "B-17/519, Indus Mehran Society",
       "contactNumber": "+923132953154"
   },
  "products": [
     {
        "name": "test10 product",
        "description": "Customize blue color vaaulid with picture printing",
        "cost": 700,
        "price": 1200,
        "weight": "2.3g"
    },
    {
        "name": "Customize vaulid",
        "description": "Customize blue color vaaulid with picture printing",
        "cost": 700,
        "price": 1200,
        "weight": "2.3g"
    }
  ]
}

I am getting this error

UpdateValuesMissingError: Cannot perform update query because update values are not defined. Call "qb.set(...)" method to specify updated values.

{"statusCode":500,"timestamp":"2023-02-25T18:06:43.623Z","path":"/orders","method":"POST","errorName":"UpdateValuesMissingError","message":"Cannot perform update query because update values are not defined. Call \"qb.set(...)\" method to specify updated values."}

How can i fix this error?



from I am struggling to save data into postgreSql table using typeORM and Nestjs with one to many relationship

No comments:

Post a Comment