Skip to content
Advertisement

Not return nested objects

I hav a problem with typeorm, I ahve na queryBuilder like below:

        const projects = await this.conn.getRepository(UserProjectRelations).createQueryBuilder("userProject")
            .innerJoin("userProject.userId", "user", "user.id = :userId", { userId: currentUser.id })
            .leftJoinAndSelect("userProject.projectId", "project")
          

        return await projects.getRawOne();

why this query return me this data:

RowDataPacket {
  userProject_id: 67,
  userProject_status: 'Owner',
  userProject_lastUpdate: 2021-03-24T21:13:32.000Z,
  project_id: 21,
  project_name: 'nest',
  project_appName: 'Test123!',
  project_createdAt: 2021-03-24T20:47:40.000Z,
  project_shortDescription: null,
}

instead of this:

{
    userProject: {
      "id":67,
      "status":"Owner",
      "lastUpdate":"..."
    },
    project: {
      "name":"test",
      "appName":"Test123!",
      "createdAt": "..."
    }
}

i need to return nested object instead of using alias, can someone tell me how to do this with using queryBuilder?

thanks for any help!

Advertisement

Answer

You have to use getOne not getRawOne.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement