有一个值表:
comment_id | user_Id | product_id | parent_id | reply_id | ... |
--------------------------------------------------------------------
164 | 7 | 40 | null | null | ... |
165 | 7 | 40 | 164 | 164 | ... |
166 | 7 | 40 | 164 | 164 | ... |
167 | 20 | 40 | 164 | 164 | ... |
168 | 20 | 40 | 164 | 164 | ... |
169 | 20 | 40 | 164 | 164 | ... |
170 | 7 | 40 | 180 | 180 | ... |
我定义了哪个父母来对待孩子的评论:
let comments = data_comments.rows;
let comments_array = [];
let tmp = {};
for (let c of comments) {
if (c.parent_id === null) {
comments_array.push(c);
tmp[c.comment_id] = c;
tmp[c.comment_id].nested_comments = [];
} else {
tmp[c.parent_id].nested_comments = tmp[c.parent_id].nested_comments || [];
tmp[c.parent_id].nested_comments.push(c);
}
}
但是我的问题是,当父母没有找到应用程序时,会失败并显示错误:
TypeError: Cannot read property 'nested_comments' of undefined
此处有170条评论没有这样的父项,因此申请被取消
如何确保在辅助注释中没有指定任何父母时,申请不会落空?
然后,我删除了注释170,因为应用程序掉落了 我传递给客户端的格式:
"productDetails": {
"product_id": 40,
"product_name": "slippers",
"product_description": "this slippers is good",
"original_price": 7600,
"sale_price": 4500,
"discount": 41,
"creating_date": "2019-02-28T04:43:04.000Z",
"end_date": "2019-03-12T07:02:19.000Z",
"date_period": "Mar 7 - Mar 12",
"product_photos": [
"https://..."
],
"quantity_available": 150,
"store_id": 9,
"store_name": "Name Store",
"contact_number": "123",
"type_location": "SITY",
"categorie": "LAPTOPS COMPUTERS",
"starting_date": "2019-03-07T07:02:19.000Z",
"likes": "0",
"bookmarks": false,
"is_liked": false,
"comments": [
{
"comment_id": 164,
"user_id": 7,
"product_id": 40,
"parent_id": null,
"reply_id": null,
"user_name": "Name Parent",
"user_avatar": "https://...,
"user_comment": "like product",
"date_comment": 1551693424,
"nested_comments": [
{
"comment_id": 169,
"user_id": 20,
"product_id": 40,
"parent_id": "164",
"reply_id": "164",
"user_name": "Name",
"user_avatar": "https://...",
"user_comment": "like product",
"date_comment": 1551693567
},
{
"comment_id": 168,
"user_id": 20,
"product_id": 40,
"parent_id": "164",
"reply_id": "164",
"user_name": "Name",
"user_avatar": "https://...",
"user_comment": "like product",
"date_comment": 1551693559
},
{
"comment_id": 167,
"user_id": 20,
"product_id": 40,
"parent_id": "164",
"reply_id": "164",
"user_name": "Name",
"user_avatar": "https://...",
"user_comment": "like product",
"date_comment": 1551693542
},
{
"comment_id": 166,
"user_id": 7,
"product_id": 40,
"parent_id": "164",
"reply_id": "164",
"user_name": "Name",
"user_avatar": "https://...",
"user_comment": "like product",
"date_comment": 1551693480
},
{
"comment_id": 165,
"user_id": 7,
"product_id": 40,
"parent_id": "164",
"reply_id": "164",
"user_name": "Name",
"user_avatar": "https://...",
"user_comment": "like product",
"date_comment": 1551693457
}
],
"is_last_page": true
}
]
}