首页 > 解决方案 > 为什么在 TypeORM 中 where 和结合 Query Builder 不起作用?

问题描述

我正在使用 typeorm 的查询生成器。

它适用于我的where功能,并且适用于该take功能。它在哪里返回 5 个项目(这是正确的),第一个是3返回 3 个项目。但它不适用于两者一起使用。然后它总是只返回一项。我究竟做错了什么?

public async search(first?: number): Promise<Item[]> {
    const findConditions: FindConditions<Item> = {
        deleted: IsNull(),
    };

    return this.itemRepository
        .createQueryBuilder()
        .select("item")
        .from(Item, "item")
        .where(findConditions)
        .take(first)
        .getMany();
}

标签: typescriptgraphqltypeorm

解决方案


我的语法错误。因为我将查询生成器与我的itemRepository一起使用,我不必使用selectandfrom函数,因为这会创建错误的FROM语句。

所以这有效:

public async search(first?: number): Promise<Item[]> {
    const findConditions: FindConditions<Item> = {
        deleted: IsNull(),
    };

    return this.itemRepository
        .createQueryBuilder()
        .where(findConditions)
        .take(first)
        .getMany();
}

推荐阅读