Saturday 10 June 2017

How do i format mysql query to utilize async/promsies?

I'm trying to make a simple query on my database with 2 parameters, and I'm having trouble trying to reformat this code using Promises/Async to avoid callback hell. Here is the code:

module.exports = {
  getDailyRaw: function(school, meal, callback) {
     var sql = "SELECT * FROM daily WHERE school = ? AND meal = ?";
     pool.getConnection(function(err, conn) {
         if(err) { console.log(err); callback(true); return; }
         conn.query(sql, [school, meal], function(err, results) {
             conn.release();
             if(err) { console.log(err); callback(true); return; }
             callback(false, school, results);
         })
     })
  }, ....

I've been constantly trying to look up tutorials on fixing this issue, but haven't been able to implement/understand them properly.



via Kevin Lee

No comments:

Post a Comment