即使在调用查找或查找功能时,即使它们之间存在关系,实体即使它们之间有关系
我正在尝试创建一个名为“作者”的实体,该实体具有已有现有实体的数组,称为书籍,返回值返回所需值,但是当我尝试查看创建的实体时,书籍的数组以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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需为您的作者实体添加渴望即可。
这样:
如果没有急切的态度,当您调用find()方法时,它将无法自动加入。
just add eager to you author entity.
Like this :
without the eager, it won't be able to join automatically when you call the find() method.