0

I am calling the following function which is in the other file. This data comes from a MySQL call. When I call this function I get "undefined value, this functions does not returning any value".

var employee_fun = require('./public/Master/manager.js');
function getJobType(param){       
    return employee_fun.getJobType(param,function(res){
        return res
    });
}

manager.js

module.exports={
    getJobType(data,callback){
        var sql="SELECT id FROM employee_types where jobtype='"+data+"'";
        mysqlcon.query(sql, function (err, result, fields) {
            callback(result[0].id);
            if (err) throw err;   
        });
    }
}
Nic3500
  • 8,144
  • 10
  • 29
  • 40
Pratik
  • 11
  • 1
  • 6
  • 2
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – tkausl Sep 16 '18 at 11:33

1 Answers1

0

Adding return statements like below will solve your issue:-

module.exports= {

  getJobType(data,callback){
    var sql="SELECT id FROM employee_types where jobtype='"+data+"'";
    return mysqlcon.query(sql, function (err, result, fields) {            
        if (err) throw err;
        return callback(result[0].id);               
    });
  }
}
Shuki
  • 290
  • 2
  • 11
  • could you please add logs and trace your request flow? it's possible that result of mysqlcon.query is undefined ... – Shuki Sep 17 '18 at 11:00