Reputation: 382
Hi i have a problem with my typeorm code.
I followed the example from here: https://github.com/typeorm/typeorm/blob/master/docs/tree-entities.md#materialized-path-aka-path-enumeration.
So my entity looks like:
import {
Entity,
PrimaryGeneratedColumn,
Column,
CreateDateColumn,
UpdateDateColumn,
Tree,
TreeChildren,
TreeParent,
} from 'typeorm';
@Entity()
@Tree('materialized-path')
export class Category {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@TreeChildren()
children: Category[];
@TreeParent()
parent: Category;
@CreateDateColumn({
type: 'timestamp',
default: () => 'CURRENT_TIMESTAMP(6)',
})
public createdAt: Date;
@UpdateDateColumn({
type: 'timestamp',
default: () => 'CURRENT_TIMESTAMP(6)',
onUpdate: 'CURRENT_TIMESTAMP(6)',
})
public updatedAt: Date;
}
In my controller called 'CategoryController' i want to get the whole tree, but i get an error that 'findAncestors' not exists.
import { getRepository } from 'typeorm';
import {Category} from "../entity/Categories";
export class CategoryController {
private categoryRepository = getRepository(Category);
async getCategoryTreeBySingleCategory(category:Category|string){
{...}
return await this.categoryRepository.findAncestors(category); // <--- Property 'findAncestors' does not exist on type 'Repository '.
}
}
Does someone know why and may help me?
Thanks in advance!
Upvotes: 2
Views: 2743
Reputation: 539
If someone is facing a problem when he writes:
Category.find()
and get error find() is not defined or something like that then,
Import BaseEntity from typeorm
export class Category extends BaseEntity
Upvotes: 0
Reputation: 4995
The member function findAncestors
is only defined on type TreeRepository
which you get by using getTreeRepository
instead of getRepository
.
Therefore write
import { getRepository } from 'typeorm';
import {Category} from "../entity/Categories";
export class CategoryController {
private categoryRepository = getTreeRepository(Category);
async getCategoryTreeBySingleCategory(category:Category|string){
{...}
return await this.categoryRepository.findAncestors(category);
}
}
Upvotes: 3