即使在调用查找或查找功能时,即使它们之间存在关系,实体即使它们之间有关系

发布于 2025-02-13 04:20:35 字数 2622 浏览 2 评论 0原文

我正在尝试创建一个名为“作者”的实体,该实体具有已有现有实体的数组,称为书籍,返回值返回所需值,但是当我尝试查看创建的实体时,书籍的数组以null值出现。 这是我用于创建作者实体

  async create(createAuthorDto: CreateAuthorDto) {
    try {
      const found: Author[] = await this.authorRepo.find({
        where: {
          FirstName: createAuthorDto.FirstName,
          LastName: createAuthorDto.LastName,
        },
      });
      if (found.length != 0) throw Error('This author already exists');
      else {
        const booklist: Book[] = await this.createArray(createAuthorDto.Books);
        console.log('The book list is : ' + booklist);
        return await this.authorRepo.save(
          this.authorRepo.create({
            FirstName: createAuthorDto.FirstName,
            LastName: createAuthorDto.LastName,
            Books: booklist,
          }),
        );
      }
    } catch (error) {
      return error.message;
    }
  }

创建书籍实体数组的代码的代码:

  async createArray(booknames: string[]) {
    const books: Book[] = [];
    for (const bookname of booknames) {
      const book: Book | undefined = await this.bookService.findOne(bookname);
      books.push(book);
      console.log(books);
    }
    console.log('The returned value: ' + books);
    return books;
  }

作者实体的代码:

import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  ManyToMany,
  JoinTable,
} from 'typeorm';

import { Book } from '../entities/book.entity';

@Entity()
export class Author {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  FirstName: string;

  @Column()
  LastName: string;

  @ManyToMany(() => Book)
  @JoinTable()
  Books: Book[];
  //In order to create an object using the book entity I need to define constructors.
  constructor(FirstName: string, LastName: string, Books: Book[]);
  constructor(FirstName: string, LastName: string, Books: Book[], id?: number) {
    this.FirstName = FirstName || '';
    this.LastName = LastName || '';
    this.Books = Books || null;
    this.id = id || null;
  }
}

使用Postman的输出结果的某些图片:

第一张图片包含作者实体的主体,

第二个包含作者实体创建并保存之后。

第三张图片是带有空书数组的实体。

I'm trying to create an entity called author that have an array of already existing entities called books,The function returning value returns the desired value but when I try and see the created entity, the array of book appears with a value of null.
This the code I used for creating the author entity

  async create(createAuthorDto: CreateAuthorDto) {
    try {
      const found: Author[] = await this.authorRepo.find({
        where: {
          FirstName: createAuthorDto.FirstName,
          LastName: createAuthorDto.LastName,
        },
      });
      if (found.length != 0) throw Error('This author already exists');
      else {
        const booklist: Book[] = await this.createArray(createAuthorDto.Books);
        console.log('The book list is : ' + booklist);
        return await this.authorRepo.save(
          this.authorRepo.create({
            FirstName: createAuthorDto.FirstName,
            LastName: createAuthorDto.LastName,
            Books: booklist,
          }),
        );
      }
    } catch (error) {
      return error.message;
    }
  }

The code for creating the book entity array :

  async createArray(booknames: string[]) {
    const books: Book[] = [];
    for (const bookname of booknames) {
      const book: Book | undefined = await this.bookService.findOne(bookname);
      books.push(book);
      console.log(books);
    }
    console.log('The returned value: ' + books);
    return books;
  }

The code of author entity:

import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  ManyToMany,
  JoinTable,
} from 'typeorm';

import { Book } from '../entities/book.entity';

@Entity()
export class Author {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  FirstName: string;

  @Column()
  LastName: string;

  @ManyToMany(() => Book)
  @JoinTable()
  Books: Book[];
  //In order to create an object using the book entity I need to define constructors.
  constructor(FirstName: string, LastName: string, Books: Book[]);
  constructor(FirstName: string, LastName: string, Books: Book[], id?: number) {
    this.FirstName = FirstName || '';
    this.LastName = LastName || '';
    this.Books = Books || null;
    this.id = id || null;
  }
}

Some pictures of the output results using postman:

The first picture contains the body for the author entity

The second contains the author entity after it was created and saved.

The third picture is the same entity with an empty book array.
Creating the author post requestThe server return the output of the save method

The author entity missing the book array although it was created and saved

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

短叹 2025-02-20 04:20:35

只需为您的作者实体添加渴望即可。
这样:

import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  ManyToMany,
  JoinTable,
} from 'typeorm';

import { Book } from '../entities/book.entity';

@Entity()
export class Author {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  FirstName: string;

  @Column()
  LastName: string;
//right here!
  @ManyToMany(() => Book,{eager:true})
  @JoinTable()
  Books: Book[];
  //In order to create an object using the book entity I need to define constructors.
  constructor(FirstName: string, LastName: string, Books: Book[]);
  constructor(FirstName: string, LastName: string, Books: Book[], id?: number) {
    this.FirstName = FirstName || '';
    this.LastName = LastName || '';
    this.Books = Books || null;
    this.id = id || null;
  }
}

如果没有急切的态度,当您调用find()方法时,它将无法自动加入。

just add eager to you author entity.
Like this :

import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  ManyToMany,
  JoinTable,
} from 'typeorm';

import { Book } from '../entities/book.entity';

@Entity()
export class Author {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  FirstName: string;

  @Column()
  LastName: string;
//right here!
  @ManyToMany(() => Book,{eager:true})
  @JoinTable()
  Books: Book[];
  //In order to create an object using the book entity I need to define constructors.
  constructor(FirstName: string, LastName: string, Books: Book[]);
  constructor(FirstName: string, LastName: string, Books: Book[], id?: number) {
    this.FirstName = FirstName || '';
    this.LastName = LastName || '';
    this.Books = Books || null;
    this.id = id || null;
  }
}

without the eager, it won't be able to join automatically when you call the find() method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文