0

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
                        }
                    }
                )
          }
                
        
       }
    });    
  }
1
  • 1
    Inside getClientId you are having another funtion insidie which the value is returned.. this wont be returned by the getClientId and this would be called async way whent he query completes. Try to switch to asyc await or promise style to get the desired results Commented Aug 24, 2020 at 10:42

1 Answer 1

1

You can convert your function to Promise and then use async/await it will be much more readable in terms of using async call functions

getClientId(con, data,callback) { 
return new Promise((resolve,reject)=>{
   var client =data.client.toLowerCase()
   con.query(`SELECT *  from client where lower(name) = '${client}'`, function(err, rows) {
   if(err) {
     reject(err)
   }
   else{ 
     if(rows.length>0){
        resolve(rows[0].id)
     }
     else{
           con.query(
             `INSERT INTO client SET 
               name = '${data.client}', 
               created_by = '${data.user_id}'`,
               function(er, row) {
                 if(er){
                   reject(er)
                 }
                   else{
                     resolve(row.insertId)
                   }
               }
           )
     }
  }
 });    
 }
)
}

and then consume this function as

const clientId = await this.getClientId(con, data,callback);
 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.