首页 > 解决方案 > (Typeorm/Postgres)从关系实体(产品)保存树实体(类别)导致错误

问题描述

使用类别名称创建产品,返回 MaterializedPathSubjectExecutor 错误。

创建类别作品。但是尝试使用关系类别保存产品会引发错误。

尝试添加到 @OneToMany()

{
persistence:false, cascade:false, eager:true
}

还在关系的两侧尝试了@JoinColumn()

类别检索功能

  async getCategories(category: string) {
    const manager = getManager();
    const cat = await manager
      .getRepository(Category)
      .findOne({
        name: Like(category),
      })
      .catch((error) => {
        console.error(error);
      });

    console.log(cat);
    if (!cat) {
      const cat = new Category();
      cat.name = category;
      cat.description = category;

      return await manager.getRepository(Category).save(cat);
    }

    return cat;
  }

保存功能

  async create(createdBy: string, productDto: CreateProductDto) {

         const { name, price, isActive, description, category, images } = productDto;
        
            const product = new Product();
        
            product.name = name;
            product.price = price;
            product.isActive = !!isActive;
        
            const creator = await this.usersService.findForId(createdBy);
            product.createdBy = creator;
            product.updatedBy = creator;
            product.description = description;
        
            product.images = await this.imageService.upload(images);
        
             if (category) {
              console.log('WHAT');
              product.category = await this.getCategories(category);
             }   
    return await this.productsRepository.save(product).catch((err) => {
          console.error(err);
          console.log(product);
        });

产品实体.ts

    import { Image } from 'src/modules/image/entity/image.entity';
    import { User } from 'src/modules/user/user.entity';
    import { BaseEntity } from 'src/utility/entity/base.entity';
    import {
      Column,
      Entity,
      JoinColumn,
      ManyToOne,
      OneToMany,
      Tree,
    } from 'typeorm';
    
    @Entity()
    @Tree('materialized-path')
    export class Product extends BaseEntity {
      @Column()
      name: string;
    
      @Column()
      price: number;
    
      @Column({ type: 'varchar', length: 300 })
      description: string;
    
      @OneToMany(() => Image, (image) => image.product)
      images: Image[];
    
      @ManyToOne((type) => Category, (category) => category.products)
      category: Category;
  @ManyToOne((type) => User, (user) => user.products_created, { lazy: true })
  @JoinColumn()
  createdBy: User;

  @ManyToOne((type) => User, (user) => user.products_created, { lazy: true })
  @JoinColumn()
  updatedBy: User;
    
    }

类别实体.ts

    @Entity({ name: 'category' })
    @Tree('materialized-path')
    export class Category extends BaseEntity {
      @Column({ type: 'varchar', length: 300 })
      /**
       * Name of category
       * @example 'book'
       * */
      name: string;
    
      @Column({ type: 'varchar', length: 300 })
      description: string;
    
      @TreeChildren()
      children: Category[];
    
      @TreeParent()
      parent: Category;
    
      @OneToMany((type) => Product, (product) => product.category)
      products: Product[];
  @ManyToOne((type) => User, (user) => user.category_created, { lazy: true })
  @JoinColumn()
  createdBy: User;

  @ManyToOne((type) => User, (user) => user.category_updated, { lazy: true })
  @JoinColumn()
  updatedBy: User;

    }

错误:已生成

TypeError: Cannot read property 'getEntityValue' of undefined
    at MaterializedPathSubjectExecutor.<anonymous> 

标签: postgresqlnestjstypeorm

解决方案


推荐阅读