如何制作评论系统nodejs + mysql

时间:2016-06-22 13:27:54

标签: javascript mysql node.js express

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', 
            }] 

}

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

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);
      });
}
  

错误总是[TypeError:无法读取属性'child'的   未定义]

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

但我在这个代码位置运行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);
      });
    }

这是通过命令行

的结果
{
    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' 
}