I have a function called uploadData in which i am calling getClientId to get the clientid. Inside the getclientId function i can get the return value. But i cant access that return value in the uploadData function. In console i am getting undefined.
uploadData(con, data, callback) {
var total = parseFloat(data.pricelist) + parseFloat(data.print)
var ac = parseFloat(total)*parseFloat(data.quantity)
var sold = parseFloat(data.soldprice)*parseFloat(data.quantity)
var rd = parseFloat(data.sold) - parseFloat(total)
var rt = parseFloat(rd) * parseFloat(data.quantity)
var client =data.client.toLowerCase()
var clientId = this.getClientId(con, data,callback)
console.log(clientId)
}
getClientId(con, data,callback) {
var client =data.client.toLowerCase()
con.query(`SELECT * from client where lower(name) = '${client}'`, function(err, rows) {
if(err) {
return err
}
else{
if(rows.length>0){
return rows[0].id
}
else{
con.query(
`INSERT INTO client SET
name = '${data.client}',
created_by = '${data.user_id}'`,
function(er, row) {
if(er){
return er
}
else{
return row.insertId
}
}
)
}
}
});
}
getClientIdyou are having another funtion insidie which the value is returned.. this wont be returned by thegetClientIdand this would be called async way whent he query completes. Try to switch toasycawaitorpromisestyle to get the desired results