TypeORM:将 Repository.create 与 NestJS 一起用于一个实体和 dto 而不是另一个实体时,出现“没有重载与此调用匹配”错误

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

问题

嘿,所以我已经多次把头靠在墙上来问这个问题了。我在任何地方都找不到解决方案,我很困惑。

由于某种原因,在以下代码中,

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)

据我所知。 CustomerAddresses 中外键 ID 的实现类似于 Menus 中餐厅外键。谁能告诉我发生了什么事吗?我被困住了。

代码

我有这两个实体:

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


我不知道该怎么办。我到处都看过了,但没发现任何异常。

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

你应该添加

    TypeOrmModule.forFeature([Menu]),

在您的导入模块中

© www.soinside.com 2019 - 2024. All rights reserved.