Search code examples
typeormnode.js-typeorm

TypeORM: What is the difference between getRawMany() and getMany()?


I have this code:

  let otherParticipants = await chatRoomParticipantRepo
    .createQueryBuilder("chatRoomParticipant")
    .leftJoinAndSelect("chatRoomParticipant.user", "user")
    .select("user.connectionId")
    .where("chatRoomParticipant.chatRoomId = :chatRoomId AND chatRoomParticipant.userId != :userId")
    .setParameters({ chatRoomId: chatRoomId, userId: userId })
    .getRawMany();

Which gives me this result:

[{user_connectionId: 's7p7bUbzt0wudKcxAAAD'}]

If I do it with getMany() it gives me empty array:

[]

I am trying to get the ID from user which connected to chatRoomParticipant, but I cannot do it with this code:

  let otherParticipants = await chatRoomParticipantRepo
    .createQueryBuilder("chatRoomParticipant")
    .leftJoinAndSelect("chatRoomParticipant.user", "user")
    .select("user.connectionId")
    .where("chatRoomParticipant.chatRoomId = :chatRoomId AND chatRoomParticipant.userId != :userId")
    .setParameters({ chatRoomId: chatRoomId, userId: userId })
    .getMany(); <-- This gives nothing

Why is this happening? I thought getRawMany() is just for SUM, COUNT, etc. In this case I am trying to get the ID which should work with getMany() as well?


Solution

  • In your case, you should use getRawMany() because you want to select some specified fields of your entity,getMany() it returns array of entities concern