gpt4 book ai didi

javascript - 如何制作评论系统nodejs + mysql

转载 作者:行者123 更新时间:2023-11-29 20:51:57 25 4
gpt4 key购买 nike

嘿,我有一个数据,我将创建分层数据示例

{
id_commentForum: 1,
id_user: 1,
id_thread: 1,
comment: 'Ini Komentar Pertama',
parent: 0,
created_at: Wed Jun 22 2016 13:36:38 GMT+0700 (WIB),
updated_at: Wed Jun 22 2016 13:36:38 GMT+0700 (WIB),
name: 'fahmy Abdul Aziz',
avatar: '/images/img/avatar/100x100/201661154914.jpg',
child : [{
id_commentForum: 1,
id_user: 1,
id_thread: 1,
comment: 'Ini Komentar Pertama',
parent: 0,
created_at: Wed Jun 22 2016 13:36:38 GMT+0700 (WIB),
updated_at: Wed Jun 22 2016 13:36:38 GMT+0700 (WIB),
name: 'fahmy Abdul Aziz',
avatar: '/images/img/avatar/100x100/201661154914.jpg',
child : [{
id_commentForum: 1,
id_user: 1,
id_thread: 1,
comment: 'Ini Komentar Pertama',
parent: 0,
created_at: Wed Jun 22 2016 13:36:38 GMT+0700 (WIB),
updated_at: Wed Jun 22 2016 13:36:38 GMT+0700 (WIB),
name: 'fahmy Abdul Aziz',
avatar: '/images/img/avatar/100x100/201661154914.jpg',
}]
}
{
id_commentForum: 1,
id_user: 1,
id_thread: 1,
comment: 'Ini Komentar Pertama',
parent: 0,
created_at: Wed Jun 22 2016 13:36:38 GMT+0700 (WIB),
updated_at: Wed Jun 22 2016 13:36:38 GMT+0700 (WIB),
name: 'fahmy Abdul Aziz',
avatar: '/images/img/avatar/100x100/201661154914.jpg',
}]

}

但是当我运行脚本时我很累,总是未定义这是我的根脚本

function rootComment(callback){
var sql = "SELECT a.*, b.name as name, b.avatar as avatar \
FROM forum_commentar a \
JOIN users b ON a.id_user = b.id \
JOIN forumthread c ON a.id_thread = c.idThread \
WHERE a.parent = 0 ORDER BY a.created_at DESC LIMIT 10";
DB.query(sql)
.then(function(result){
var child = '';
for (var i = 0; i < result.length; i++) {
getChild(result[i].id_commentForum, function(err, data){
if(err) console.log(err);
else{
if(typeof result[i].child === 'undefined'){
result[i].child = data;
}
}
});
}
retval.comment = result;
callback(null, retval);
})
.catch(function (err) {
console.log(err);
callback(err);
});
}

这是 getChild 脚本

function getChild(id, cb){
var sql2 = "SELECT a.*, b.name as name, b.avatar as avatar FROM forum_commentar a \
JOIN users b ON a.id_user = b.id \
JOIN forumthread c ON a.id_thread = c.idThread \
LEFT JOIN forum_commentar d ON a.id_commentForum = d.parent \
WHERE a.parent = '"+id+"' ORDER BY a.created_at DESC LIMIT 10";
DB.query(sql2)
.then(function(result){
return cb(null, result);
})
.catch(function (err) {
return cb(err, null);
});
}

the error always [TypeError: Cannot read property 'child' of undefined]

最佳答案

DB.query(sql2)的结果似乎是未定义的。您检查 result[i].child === 'undefined' 是否,但 result 未定义。

关于javascript - 如何制作评论系统nodejs + mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37969537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com