【发布时间】:2021-11-25 06:06:12
【问题描述】:
在尝试返回所有 cmets 以及对 cmets 的回复时,我遇到了问题
示例模板:
[
{
"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"
}
]
所以,我得到了什么:
[
{
"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"
}
]
获取所有 cmets 的代码:
@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,
);
}
在 getReply 方法中,我使用控制台进行调试。这是里面出现的内容
[
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'
}
]
[]
所以,在我得到“replyId”字段的响应中:= {},有一个空对象。我该如何解决这个问题?
提前感谢您的回答!
【问题讨论】:
-
您能否将代码减少到所需的最低限度(没有任何数据库访问代码),因为您手头似乎已经有了模型?这将允许其他人快速测试您的代码并帮助您找到解决方案
-
控制台日志中似乎没有空对象..你的意思是空数组
[]? -
@ParvSharma 我复制了 IDE 控制台中的所有内容
标签: javascript arrays object async-await