【问题标题】:typeorm leftJoinAndSelect get one item of relation arraytypeorm leftJoinAndSelect 获取关系数组的一项
【发布时间】:2022-01-22 13:00:24
【问题描述】:

我是韩国学生。请理解我糟糕的英语。

我正在尝试与参与者建立聊天室 并且 chatting_room 有已登录用户的参与者。

这是我的代码

await this.Repository
    .createQueryBuilder("room")
    .leftJoinAndSelect("room.owner", "owner")
    .leftJoinAndSelect("room.matching", "matching")
    .leftJoinAndSelect("room.participants", "participants")
    .where("participants.userId = :userId", { userId: loggedInUser.userId })
    .getMany()

chatting_room 中有两个参与者 (ManyToMany) 但结果只显示了一个已登录用户 我想获得所有参与者。 问题是什么 ? 请帮帮我!

【问题讨论】:

    标签: javascript typescript typeorm


    【解决方案1】:

    我现在就用这段代码

    const participatedList = await this.chatting_roomDB
            .createQueryBuilder("room")
            .leftJoinAndSelect("room.owner", "owner")
            .leftJoinAndSelect("room.matching", "matching")
            .leftJoinAndSelect("room.participants", "participants")
            .where("participants.userId = :userId", { userId: loggedInUser.userId })
            .getMany()
    
          const participatedRoomIds = await participatedList.map((room) => {
            return { chatting_roomId: room.chatting_roomId }
          })
    
          const participatedRooms = await this.chatting_roomDB.find({
            where: participatedRoomIds,
            relations: ["owner", "participants", "matching"],
          })
    

    太糟糕了!哈哈,给我一个好代码!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-17
      • 1970-01-01
      • 1970-01-01
      • 2020-06-25
      • 1970-01-01
      • 2021-11-15
      相关资源
      最近更新 更多