Search code examples
javascriptnode.jsnestjstypeorm

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

constructor(
  @InjectRepository(Order)
  private orderRepository: Repository<Order>,
) {}


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?


Solution

  • you need to create the customer frist before you save or get the customer if he is saved products is same issue

    async createOrder(order: CreateOrderDto) {
      // you can make it transaction to applay (ACID)
      const customer = await this.customerRepository.save(order.customer);
      //  if customer is already saved in database not a new entity only get this entity by id or what ever
      //  const customer =  await this.customerRepository.findOne({where : {id :order.customer.id}});
      const products = await this.productsRepository.find({where : {id : In(customer.products.map(p=>p.id))}}
      const newOrder = await this.orderRepository.save({...order, customer : newCustomer, products});
      return newOrder;
    }