Javascript 应用程序找不到父级时崩溃
有一个包含值的表:Javascript 应用程序找不到父级时崩溃,javascript,node.js,Javascript,Node.js,有一个包含值的表: comment_id | user_Id | product_id | parent_id | reply_id | ... | -------------------------------------------------------------------- 164 | 7 | 40 | null | null | ... | 165 | 7 | 4
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
}
]
}
正如我在问题注释中所述,您正在尝试访问未定义变量的属性。您可以通过使用两个循环来解决这个问题,这两个循环将正确地填充
注释\u数组
试试这个(未经测试):
嵌套注释是注释的一部分吗?不,在这个字段中,我写下子注释并以JSONY格式传递。您的代码似乎认为
c.comment\u id
和c.parent\u id
在某种程度上是匹配的,但我认为它们不匹配。请详细说明,您能否共享添加的格式。如果没有错误消息,我应该如何帮助您?错误与TypeError相同:无法读取未定义的属性“嵌套注释”
您可以将数据注释的内容粘贴到中吗?然后我可以重新创建一个工作示例。我按照您的要求做了,在izi comment 492的示例中,整个应用程序崩溃了
let comments = data_comments.rows;
let comments_array = [];
let tmp = {};
for (let c of comments) {
tmp[c.comment_id] = c;
c['nested_comments'] = [];
}
for (let c of comments) {
if (c.parent_id !== null && tmp[c.parent_id] !== undefined) {
tmp[c.parent_id]['nested_comments'].push(c);
} else {
comments_array.push(c);
}
}