如何使用 Node.js 组合 2 个结果的响应

问题描述 投票:0回答:1

下面的代码是获取与特定 id 和此 service.ts 文件关联的用户列表。

async getId(id:number){
//below code will give us participants list based on id
let participants = await this.participantRepo.findById(id)

//here I am looping each participant to get their profile picture if it's available
   for (const participant of participants) {
      let result = await this.profilePictureService.getprofilePicture(participant.user);
      //here in **result** I will get profile picture of each user if it's available
    }

const [intParticipants, extParticipants] = _partition(
      participants,
      (p: CampParticipants) => p.participantRoleId !== ParticipantRoleEnum.External
    );

 return {
      internalParticipants,
      externalParticipants,
    };

}

参与者的回复示例:

{
  "internalParticipants": [
    {
      "id": 515,
      "userId": 126,      "
      "participantRoleId": 1,
      "user": {
        "id": 126,
        "firstName": "Test1",
        "lastName": "Test",
        "mail": "[email protected]",
       }
    },
    {
      "id": 515,
      "userId": 1219,
      "participantRoleId": 2,
       "user": {
        "id": 1219,
        "firstName": "Rob",
        "lastName": "const",
        "mail": "[email protected]",        
      }
    }
],
  "externalParticipants": []
}

个人资料图片回复示例:

{
"photoName": "profile-picture.png",
"buffer": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABDgAAAQ4CAYAAADsEGyPAAAAAXNSR0IArc"
}

假设我们有仅与“userId”相关联的个人资料图片:126,如何合并两个响应以获得如下结果?

如何将个人资料图片回复与参与者列表回复结合起来? **

  • 最终响应/输出响应

**

{
  "internalParticipants": [
    {
      "id": 515,
      "userId": 126,      "
      "participantRoleId": 1,
      "user": {
        "id": 126,
        "firstName": "Test1",
        "lastName": "Test",
        "mail": "[email protected]",
       }
      "photoName": "profile-picture.png",
      "buffer": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABDgAAAQ4CAYAAADsEGyPAAAAAXNSR0IArc"
    },
    {
      "id": 515,
      "userId": 1219,
      "participantRoleId": 2,
       "user": {
        "id": 1219,
        "firstName": "Rob",
        "lastName": "const",
        "mail": "[email protected]",        
      }
    },
  "externalParticipants": []
}
javascript node.js nestjs typeorm
1个回答
0
投票
participants.map(async (pt) => {
          pt.image = await this.profilePictureService.getprofilePicture(participant.user)
          return pt; 
        }));

您可以通过参与者进行映射,这将添加一个包含图像响应对象的附加关键图像。或者,如果您想将其分开,只需将逻辑更改为内部地图下方即可。

let image = await this.profilePictureService.getprofilePicture(participant.user)
pt.photoName = image.photoName;
pt.buffer= image.buffer;
return pt;
© www.soinside.com 2019 - 2024. All rights reserved.