我正在努力使用具有一对多关系的 typeORM 和 Nestjs 将数据保存到 postgreSql 表中

问题描述 投票:0回答:1

我有 2 个实体,即具有一对多关系的客户、订单。一个客户可以有多个订单,一个订单只能由一个客户拥有。我正在创建一个 api 来创建一个

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>;
}

我从邮递员那里发送这个 json

{
   "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"
    }
  ]
}

我收到这个错误

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."}

我该如何解决这个错误?

javascript node.js nestjs typeorm
1个回答
0
投票

您需要在保存之前先创建客户,或者如果他已保存则获取客户 产品是一样的问题

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;
}
© www.soinside.com 2019 - 2024. All rights reserved.