Saturday, 15 April 2017

Nodejs Mysql callback in callback

Hello I'm stuck in my first callback "selectArticleByTitle(title, callback)", the terminal send "Cannot read property 'id' of undefined". I don't know how to force the first callback to finish this and launch the others.

 router.get('/article/:title', function(req, res){
    dataBase.selectArticleByTitle(req.params.title, function(db_titleERR, db_titleResults){
      dataBase.selectArticle(db_titleResults[0].id, function(db_resultsArticleERR, db_resultsArticle) {
        //Get id of the previous article
        dataBase.previousArticle(db_titleResults[0].id, function(db_previousIdERR, db_previousId){
          //Get id of the next article
          dataBase.nextArticle(db_titleResults[0].id, function(db_nextIdERR, db_nextId){
            //Get lastArticle
            dataBase.lastArticle(function(db_lastArticleERR, db_lastArticle) {
              });
            });
          });
        });
      });
    });
});



exports.selectArticleByTitle = function(title, callback){
  connection.query('select * from article where title=?', [title], function(err, row){
    if(err)
      callback(err, null);
    else{
      if(row){
        callback(null, row);
      }
    }
  });
}

Thank you in advance



via David Manutea

No comments:

Post a Comment