NodeJS 我很难使用具有一对多关系的typeORM和Nestjs将数据保存到postgreSql表中

epfja78i  于 2023-03-01  发布在  Node.js
关注(0)|答案(2)|浏览(253)
    • bounty将在6天后过期**。回答此问题可获得+50声望奖励。Fahad Subzwari希望引起更多人关注此问题。

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

我从 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"
    }
  ]
}

我收到此错误

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

我如何修复此错误?

tvmytwxo

tvmytwxo1#

您需要先创建客户,然后再保存或获取客户(如果已保存),产品是同一问题

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

x4shl7ld2#

您需要像这样定义表顺序和客户之间的关系。

import { Entity, ManyToOne } from "typeorm"
import { Customer } from "./Customer" 
 
 @Entity()
export class Order {

   @ManyToOne(() => Customer, (customer) => customer.order)
    customer: Customer
    
}

希望对你有帮助。

相关问题