Home > Back-end >  I get an empty array
I get an empty array

Time:10-11

When trying to return all comments, as well as replies to comments, I ran into the problem

Example template:

[
  {
    "id": "4f7b2bb3-b224-45d9-8093-9c0de7514bd4",
    "content": "comment",
    "authorId": "a30bfd0b-0519-4b4f-bbc5-04178f8af782",
    "entityId": "91e22cb7-cb42-49a3-a5e7-8d111111",
    "creatAt": "2021-10-04T08:43:04.391Z",
    "replyId": [
        {
          "id": "cab2d3fd-7fba-4d02-a911-538246d92cfd",
          "content": "comment",
          "authorId": "a30bfd0b-0519-4b4f-bbc5-04178f8af782",
          "entityId": "91e22cb7-cb42-49a3-a5e7-8d111111",
          "creatAt": "2021-10-04T08:43:22.663Z",
          "replyId": [
              {
                "id": "dee96b97-cd45-4a09-a27d-985617cc5a16",
                "content": "comment",
                "authorId": "a30bfd0b-0519-4b4f-bbc5-04178f8af782",
                "entityId": "91e22cb7-cb42-49a3-a5e7-8d111111",
                "creatAt": "2021-10-04T08:43:35.204Z",
                "replyId": "cab2d3fd-7fba-4d02-a911-538246d92cfd",
                "type": "COMMENT_TYPE"
              }
            ],
          "type": "COMMENT_TYPE"
        },
        {
          "id": "d61e0049-9075-4f25-8f6d-65ed61e245a8",
          "content": "comment",
          "authorId": "a30bfd0b-0519-4b4f-bbc5-04178f8af782",
          "entityId": "91e22cb7-cb42-49a3-a5e7-8d111111",
          "creatAt": "2021-10-04T08:43:21.271Z",
          "replyId": "4f7b2bb3-b224-45d9-8093-9c0de7514bd4",
          "type": "COMMENT_TYPE"
        },
        {
          "id": "cab2d3fd-7fba-4d02-a911-538246d92cfd",
          "content": "comment",
          "authorId": "a30bfd0b-0519-4b4f-bbc5-04178f8af782",
          "entityId": "91e22cb7-cb42-49a3-a5e7-8d111111",
          "creatAt": "2021-10-04T08:43:22.663Z",
          "replyId": "4f7b2bb3-b224-45d9-8093-9c0de7514bd4",
          "type": "COMMENT_TYPE"
        },
        {
          "id": "1ea5cdcb-2f19-4b00-94e0-5b245bb91237",
          "content": "comment",
          "authorId": "a30bfd0b-0519-4b4f-bbc5-04178f8af782",
          "entityId": "91e22cb7-cb42-49a3-a5e7-8d111111",
          "creatAt": "2021-10-04T08:43:22.094Z",
          "replyId": "4f7b2bb3-b224-45d9-8093-9c0de7514bd4",
          "type": "COMMENT_TYPE"
        }
      ],
    "type": "COMMENT_TYPE"
  }
]

So, What i get:

[
  {
    "id": "4f7b2bb3-b224-45d9-8093-9c0de7514bd4",
    "content": "comment",
    "authorId": "a30bfd0b-0519-4b4f-bbc5-04178f8af782",
    "entityId": "91e22cb7-cb42-49a3-a5e7-8d111111",
    "creatAt": "2021-10-04T08:43:04.391Z",
    "replyId": {},
    "type": "COMMENT_TYPE"
  }
]

The code with which I get all the comments:

@Get('/:entityId/comments')
  async getAll(@Param('entityId') entityId: string): Promise<any> {
    const comments = await this.commentRepository.find({
      where: { entityId: entityId, replyId: null },
      order: { id: 'DESC' },
    });
    return comments.map(entity => this.mapper(entity));
  }

private async getReply(commentId: string) {
    const reply = this.commentRepository.find({ where: { replyId: commentId } });
    reply.then(items => console.log(items));
    return reply.then(items => items.map(entity => this.mapper(entity)));
  }

  private mapper(entity: Comment): CommentDto {
    return new CommentDto(
      entity.id,
      entity.content,
      entity.authorId,
      entity.entityId,
      entity.creatAt,
      this.getReply(entity.id),
      entity.type,
    );
  }

In the getReply method I am using the console for debugging. This is what appears in it

[
  Comment {
    id: 'd61e0049-9075-4f25-8f6d-65ed61e245a8',
    content: 'comment',
    authorId: 'a30bfd0b-0519-4b4f-bbc5-04178f8af782',
    entityId: '91e22cb7-cb42-49a3-a5e7-8d111111',
    creatAt: 2021-10-04T08:43:21.271Z,
    replyId: '4f7b2bb3-b224-45d9-8093-9c0de7514bd4',
    type: 'COMMENT_TYPE'
  },
  Comment {
    id: '1ea5cdcb-2f19-4b00-94e0-5b245bb91237',
    content: 'comment',
    authorId: 'a30bfd0b-0519-4b4f-bbc5-04178f8af782',
    entityId: '91e22cb7-cb42-49a3-a5e7-8d111111',
    creatAt: 2021-10-04T08:43:22.094Z,
    replyId: '4f7b2bb3-b224-45d9-8093-9c0de7514bd4',
    type: 'COMMENT_TYPE'
  },
  Comment {
    id: 'cab2d3fd-7fba-4d02-a911-538246d92cfd',
    content: 'comment',
    authorId: 'a30bfd0b-0519-4b4f-bbc5-04178f8af782',
    entityId: '91e22cb7-cb42-49a3-a5e7-8d111111',
    creatAt: 2021-10-04T08:43:22.663Z,
    replyId: '4f7b2bb3-b224-45d9-8093-9c0de7514bd4',
    type: 'COMMENT_TYPE'
  }
]
[]
[]
[
  Comment {
    id: 'dee96b97-cd45-4a09-a27d-985617cc5a16',
    content: 'comment',
    authorId: 'a30bfd0b-0519-4b4f-bbc5-04178f8af782',
    entityId: '91e22cb7-cb42-49a3-a5e7-8d111111',
    creatAt: 2021-10-04T08:43:35.204Z,
    replyId: 'cab2d3fd-7fba-4d02-a911-538246d92cfd',
    type: 'COMMENT_TYPE'
  }
]
[]

So, in the response I get the "replyId" field: = {}, there is an empty object. How can I fix this?

Thanks in advance for your answer!

CodePudding user response:

  1. Make sure you don't have any type in CommentDto replyId.
  2. Don't use then. Async / Await came up with to get rid of .then.

Solution:

@Get('/:entityId/comments')
  async getAll(@Param('entityId') entityId: string): Promise<CommentDto[]> {
    const comments = await this.commentRepository.find({
      where: { entityId: entityId, replyId: null },
      order: { id: 'DESC' },
    });
    return await Promise.all(comments.map(entity => this.mapper(entity)));
  }

private async getReply(commentId: string): Promise<CommentDto[]> {
    const reply = await this.commentRepository.find({ where: { replyId: commentId } });
    reply.then(items => console.log(items));
    return await Promise.all(reply.map(entity => this.mapper(entity)));
  }

  private mapper(entity: Comment): Promise<CommentDto> {
    return new CommentDto(
      entity.id,
      entity.content,
      entity.authorId,
      entity.entityId,
      entity.creatAt,
      await this.getReply(entity.id),
      entity.type,
    );
  }
  • Related