0

I want to rewrite the below code using async await. The problem is while using the below code how should I return the nextInvoiceNo. as a response to my request using callbacks, because the nextInvoiceNo. value is getting lost outside the braces. And please help me to write the code using async await.

const mysql = require('mysql');
const db = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password : '',
    database : 'data2020'
});
app.get('/nextInvoiceNumber',(req,res)=>{
    let shopName = "KR";
    let counterName = "F";
    let nextInvoiceNo = 1;

    shopName = shopName.trim();
    counterName = counterName.trim();
      //get shop id
    let st =  getStoreByName(shopName,(err,result)=>{
        if(err){
            console.log(err);
        }else{
            if(result.length>0){
                result.forEach(element=>{
                     storeId = element['id'];
                });
                let sql = "Select * from store_sale where store_id = '"+storeId+"' and counter_name = '"+counterName+"' and is_delete <> '1' ORDER BY invoice_no DESC LIMIT 0,1";
                let t = db.query(sql,(err,res)=>{
                    if(err){
                      console.log(err);
                    }
                    else{
                      if(res.length>0){
                        res.forEach(element=>{
                         nextInvoiceNo = element['invoice_no']+1;
                        });
                      }
                      else{
                          console.log("No Sales Found");
                      }
                    } 
                 });
            }
            else{
                console.log("Invalid StoreName")
            }
        }

    });
    res.send("done");   
})

function getStoreByName(shopName,callback)
{
    
name=shopName.trim();
sql = "Select id,name from store_master where name = '"+name+"' AND is_delete <> '1' LIMIT 0,1"
let t = db.query(sql,(err,result)=>{
    if(err){
       callback(err,null);
    }
    else{
      callback(null,result);
    } 
 });
}


  • First make sure you use a MySQL library that supports promises and not only callbacks, then it will be straight forward. – CherryDT Aug 17 '20 at 18:00
  • @CherryDT can you please share an example or link of article about the same. – user11350030 Aug 17 '20 at 18:07
  • https://www.npmjs.com/package/mysql2#using-promise-wrapper – CherryDT Aug 17 '20 at 18:12
  • 1
    Does this answer your question? [node.js async/await using with MySQL](https://stackoverflow.com/questions/44004418/node-js-async-await-using-with-mysql) – CherryDT Aug 17 '20 at 18:13
  • node.js async/await using with MySQL, from this link I tries the mysql2 answer, when I write ` let [rows,fields] = await connection.execute("Select * from app"), I get an error ,(intermediate value ) is not iterable. How will I fetch the rows. – user11350030 Aug 17 '20 at 18:35
  • It is working for the edit code, can you tell me how will I retrieve the result in case of a select command. – user11350030 Aug 17 '20 at 18:42
  • Make sure you actually install and use the `mysql2` module and import `mysql2/promise` as in the example, not `mysql`. – CherryDT Aug 17 '20 at 19:30

0 Answers0