【问题标题】:How to return only all parent sections?如何只返回所有父部分?
【发布时间】:2021-02-23 21:24:36
【问题描述】:

我正在使用 Nestjs 作为我的后端,我正在尝试创建一个仅返回所有父部分但不知道如何执行此操作的 api 调用,因此如果我能得到任何帮助或建议,我将不胜感激。

实体

    @PrimaryGeneratedColumn({
        type: "int",
        name: "Id",
    })
    id: number;

    @Column("nvarchar", {
        nullable: false,
        unique: true,
        name: "Name"
    })
    name: string;

    @Column("nvarchar", {
        nullable: false,
        name: "ParentId"
    })
    parentId: number;

控制器

    @Get('/parentSection')
    async getSectionHelp(@Req() req, @Param() params):Promise<HelpEntity[]>{
        return this.helpService.getHelpbySection(req.user, params.id);
    }

服务

  constructor(@InjectRepository(HelpEntity) private readonly helpRepo: Repository<HelpEntity>,

    async getHelpbySection(gmid: string, id: number) : Promise<any>{
        let check = await this.checkIfAdmin(gmid);
        if (!check) {
            throw new HttpException('Unauthorized', 401);
        } else {
            const res = await this.helpRepo.find()
            if (!res) {
                throw new NotFoundException(`This ID: ${id} is not found`)
            }
            return res;
        }
    }

在我的数据库中,我所有的父部分在 ParentId 中都有 NULL。

【问题讨论】:

    标签: angular typescript api backend nestjs


    【解决方案1】:

    您只需在查找函数中指定它:

     import {IsNull} from "typeorm";
     ...
     ...
     async getHelpbySection(gmid: string, id: number) : Promise<any>{
        let check = await this.checkIfAdmin(gmid);
        if (!check) {
            throw new HttpException('Unauthorized', 401);
        } else {
            const res = await this.helpRepo.find({parentId: IsNull()})
            if (!res) {
                throw new NotFoundException(`This ID: ${id} is not found`)
            }
            return res;
        }
    }
    

    【讨论】:

    • 嗯,它工作。非常感谢Youba..我真的很感激。
    • 嗨,Youba..你也可以帮我解决这个问题,stackoverflow.com/questions/64794624/…。如果可以的话,我将不胜感激。谢谢
    • 不客气,我刚做了,你现在可以看看
    猜你喜欢
    • 1970-01-01
    • 2014-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-23
    • 2021-09-22
    • 2020-09-19
    相关资源
    最近更新 更多