Hy,I have a data, I will create hierarchical data example
Hy,我有一个数据,我将创建分层数据示例
{
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',
}]
}
but I'm tired when I run the script always undefined this is my root script
但我累了,当我运行脚本时总是未定义这是我的根脚本
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);
});
}
And this is getChild Script
这是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]
错误总是[TypeError:无法读取undefined的属性'child']
2 个解决方案
#1
0
It seems that the result of DB.query(sql2) is undefined. you check if result[i].child === 'undefined', but result is undefined.
看来DB.query(sql2)的结果是未定义的。检查结果[i] .child ==='undefined',但结果是未定义的。
#2
0
but I running console.log in this position of code
但我在这个代码位置运行console.log
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{
// show the data of getChild()
console.log(data)
}
});
}
retval.comment = result;
callback(null, retval);
})
.catch(function (err) {
console.log(err);
callback(err);
});
}
this is result via command line
这是通过命令行的结果
{
id_commentForum: 3,
id_user: 2,
id_thread: 1,
comment: 'Ini anak komentar pertama',
parent: 1,
created_at: Wed Jun 22 2016 14:01:51 GMT+0700 (WIB),
updated_at: Wed Jun 22 2016 14:01:51 GMT+0700 (WIB),
name: 'dr. Boyke Dian Nugraha SpOG, MARS.',
avatar: '/images/img/avatar/25x25/avatar.jpg'
}
#1
0
It seems that the result of DB.query(sql2) is undefined. you check if result[i].child === 'undefined', but result is undefined.
看来DB.query(sql2)的结果是未定义的。检查结果[i] .child ==='undefined',但结果是未定义的。
#2
0
but I running console.log in this position of code
但我在这个代码位置运行console.log
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{
// show the data of getChild()
console.log(data)
}
});
}
retval.comment = result;
callback(null, retval);
})
.catch(function (err) {
console.log(err);
callback(err);
});
}
this is result via command line
这是通过命令行的结果
{
id_commentForum: 3,
id_user: 2,
id_thread: 1,
comment: 'Ini anak komentar pertama',
parent: 1,
created_at: Wed Jun 22 2016 14:01:51 GMT+0700 (WIB),
updated_at: Wed Jun 22 2016 14:01:51 GMT+0700 (WIB),
name: 'dr. Boyke Dian Nugraha SpOG, MARS.',
avatar: '/images/img/avatar/25x25/avatar.jpg'
}