postgresql TypeORM:“No overload matches this call”错误当使用Repository.create和NestJS为一个实体和dto而不是另一个实体时

jv2fixgn  于 2024-01-07  发布在  PostgreSQL
关注(0)|答案(1)|浏览(194)

问题

嘿,为了问这个问题,我已经把头撞到墙上够多次了。我在任何地方都找不到解决办法,我很困惑。
由于某种原因,在下面的代码中,this.customerAddressRepository.create(data)中的数据带有下划线,并带有此错误:

No overload matches this call.
  Overload 1 of 3, '(entityLikeArray: DeepPartial<CustomerAddress>[]): CustomerAddress[]', gave the following error.
    Argument of type 'CreateCustomerAddressDto' is not assignable to parameter of type 'DeepPartial<CustomerAddress>[]'.
      Type 'CreateCustomerAddressDto' is missing the following properties from type 'DeepPartial<CustomerAddress>[]': length, pop, push, concat, and 29 more.
  Overload 2 of 3, '(entityLike: DeepPartial<CustomerAddress>): CustomerAddress', gave the following error.
    Argument of type 'CreateCustomerAddressDto' is not assignable to parameter of type 'DeepPartial<CustomerAddress>'.ts(2769)

字符串
据我所知。在CustomerData中的外键ID的实现类似于菜单中的餐厅外键。有人能告诉我发生了什么吗?我被卡住了。

代码

我有两个实体:

import { MenuItem } from 'src/menu-items/entities/menu-item.entity'
import { Restaurant } from 'src/restaurants/entities/restaurant.entity'
import { Entity, Column, PrimaryGeneratedColumn, ManyToOne, OneToMany, JoinColumn } from 'typeorm'

@Entity('menus')
export class Menu {
  @PrimaryGeneratedColumn('uuid')
  id: string

  @Column({ length: 255 })
  name: string

  @JoinColumn({ name: 'restaurant_id' })
  @ManyToOne(() => Restaurant, restaurant => restaurant.menus)
  restaurant: Restaurant

  @OneToMany(() => MenuItem, menuItem => menuItem.menu, { cascade: true, eager: true })
  menuItems: MenuItem[]
}
import { AddressDetail } from 'src/address-details/entities/address-detail.entity'
import { Customer } from 'src/customers/entities/customer.entity'
import { Entity, PrimaryGeneratedColumn, ManyToOne, JoinColumn } from 'typeorm'

@Entity('customer_addresses')
export class CustomerAddress {
  @PrimaryGeneratedColumn('uuid')
  id: string

  @JoinColumn({ name: 'customer_id' })
  @ManyToOne(() => Customer, customer => customer.addresses)
  customer: Customer

  @JoinColumn({ name: 'address_id' })
  @ManyToOne(() => AddressDetail, { eager: true })
  address: AddressDetail
}

的数据

这两个分别在其服务中创建函数:

import { Injectable } from '@nestjs/common'
import { CreateCustomerAddressDto } from './dto/create-customer-address.dto'
import { UpdateCustomerAddressDto } from './dto/update-customer-address.dto'
import { CustomerAddress } from './entities/customer-address.entity'
import { InjectRepository } from '@nestjs/typeorm'
import { Repository } from 'typeorm'
import { RpcException } from '@nestjs/microservices'

@Injectable()
export class CustomerAddressesService {
  constructor(
    @InjectRepository(CustomerAddress)
    private readonly customerAddressRepository: Repository<CustomerAddress>,
  ) {}

  async create(data: CreateCustomerAddressDto): Promise<CustomerAddress> {
    const customerAddress = this.customerAddressRepository.create(data)
    return this.customerAddressRepository.save(customerAddress)
  }
}
import { Injectable, NotFoundException } from '@nestjs/common'
import { CreateMenuDto } from './dto/create-menu.dto'
import { UpdateMenuDto } from './dto/update-menu.dto'
import { Menu } from './entities/menu.entity'
import { InjectRepository } from '@nestjs/typeorm'
import { Repository } from 'typeorm'
import { RpcException } from '@nestjs/microservices'

@Injectable()
export class MenusService {
  constructor(
    @InjectRepository(Menu)
    private readonly menuRepository: Repository<Menu>,
  ) {}
  async create(data: CreateMenuDto): Promise<Menu> {
    const menu = this.menuRepository.create(data)
    return this.menuRepository.save(menu)
  }
}

的字符串

下面是模块:

import { Module } from '@nestjs/common'
import { MenusService } from './menus.service'
import { MenusController } from './menus.controller'
import { TypeOrmModule } from '@nestjs/typeorm'
import { Menu } from './entities/menu.entity'

@Module({
  imports: [TypeOrmModule.forFeature([Menu])],
  controllers: [MenusController],
  providers: [MenusService],
})
export class MenusModule {}
import { Module } from '@nestjs/common'
import { CustomerAddressesService } from './customer-addresses.service'
import { CustomerAddressesController } from './customer-addresses.controller'
import { CustomerAddress } from './entities/customer-address.entity'
import { TypeOrmModule } from '@nestjs/typeorm'

@Module({
  imports: [TypeOrmModule.forFeature([CustomerAddress])],
  controllers: [CustomerAddressesController],
  providers: [CustomerAddressesService],
})
export class CustomerAddressesModule {}

的字符串
我不知道该怎么办。我到处都找过了,但没发现有什么出入。

qrjkbowd

qrjkbowd1#

你应该添加

TypeOrmModule.forFeature([Menu]),

字符串
在您的导入模块中

相关问题