简体   繁体   中英

async await calling function node.js

I have this function, that queries my database, and then calls getNumOfSessionForTrainerClientList function:

var select = "select * from USERS WHERE ASSIGNED_TRAINER = ?"
mysqlconn.connect(function(err) {
    if (err) {
        console.error('Database connection failed: ' + err.stack);
        return;
    }

    mysqlconn.query(select, [req.session.username], async function(err, rows) {
        if (err) {
            console.log(err);
        } else {
            
            let numOfSessionsLeft = {
                numberOfSessionsLeftIs: 0
            }
            for (var i = 0; i < rows.length; i++) {
                getNumOfSessionForTrainerClientList(req, res, rows[i], numOfSessionsLeft)
                rows[i]["NUM_OF_SESSIONS_PER_MONTH"] = (parseInt(rows[i]["NUM_OF_SESSIONS_PER_MONTH"]) - numOfSessionsLeft.numberOfSessionsLeftIs)
                console.log(numOfSessionsLeft)
            }
          

        }
        mysqlconn.end();

    })
})

and then inside the function getNumOfSessionForTrainerClientList , I have this:

async function getNumOfSessionForTrainerClientList(req, res, rows, numOfSessionsLeft) {

var getNumOfSessionForTrainerClientList = "select * from SCHEDULE WHERE CLIENT_USERNAME = ? AND ASSIGNED_TRAINER = ?"
mysqlconn.connect(async function(err) {
    if (err) {
        console.error('Database connection failed: ' + err.stack);
        return;
    }
        mysqlconn.query(getNumOfSessionForTrainerClientList, [rows["USERNAME"], req.session.username], async function(err, sessionData) {
            if (err) {
                console.log(err);
            } else {
                numOfSessionsLeft.numberOfSessionsLeftIs = 1;
                console.log(numOfSessionsLeft.numberOfSessionsLeftIs)

            }

        })

})

}

however, what is happening is that this line: rows[i]["NUM_OF_SESSIONS_PER_MONTH"] = (parseInt(rows[i]["NUM_OF_SESSIONS_PER_MONTH"]) - numOfSessionsLeft.numberOfSessionsLeftIs) is actually assigning 0 to rows[i]["NUM_OF_SESSIONS_PER_MONTH"] because that variable assignment is happening before the function call for getNumOfSessionForTrainerClientList finishes. So it is happening out of sync. I am not sure how to fix this, i have run into issues with async await before, but nothing like this. would appreciate the help.

For loop is synchronous in nature and hence doesn't wait for your method getNumOfSessionForTrainerClientList to return to continue the iteration.

The right way to do is:

  1. Put your code inside an async block and use await() while calling getNumOfSessionForTrainerClientList call.
  2. Second, your function getNumOfSessionForTrainerClientList should return a promise.

Sample:

async function functionA() {
    var select = "select * from USERS WHERE ASSIGNED_TRAINER = ?"
    mysqlconn.connect(function(err) {
        if (err) {
            console.error('Database connection failed: ' + err.stack);
            return;
        }
    
        mysqlconn.query(select, [req.session.username], async function(err, rows) {
            if (err) {
                console.log(err);
            } else {
                
                let numOfSessionsLeft = {
                    numberOfSessionsLeftIs: 0
                }
                for (var i = 0; i < rows.length; i++) {
                    await getNumOfSessionForTrainerClientList(req, res, rows[i], numOfSessionsLeft)
                    rows[i]["NUM_OF_SESSIONS_PER_MONTH"] = (parseInt(rows[i]["NUM_OF_SESSIONS_PER_MONTH"]) - numOfSessionsLeft.numberOfSessionsLeftIs)
                    console.log(numOfSessionsLeft)
                }
            }
            mysqlconn.end();
        })
    })
}

And your function getNumOfSessionForTrainerClientList should look like:

async function getNumOfSessionForTrainerClientList(req, res, rows, numOfSessionsLeft) {

    return new Promise((resolve, reject) => {

        var getNumOfSessionForTrainerClientList = "select * from SCHEDULE WHERE CLIENT_USERNAME = ? AND ASSIGNED_TRAINER = ?"
        mysqlconn.connect(async function(err) {
            if (err) {
                console.error('Database connection failed: ' + err.stack);
                return reject(err);
            }
                mysqlconn.query(getNumOfSessionForTrainerClientList, [rows["USERNAME"], req.session.username], async function(err, sessionData) {
                    if (err) {
                        console.log(err);
                        return reject(err);
                    } else {
                        numOfSessionsLeft.numberOfSessionsLeftIs = 1;
                        console.log(numOfSessionsLeft.numberOfSessionsLeftIs)
                        return resolve();

                    }
                })

        })
    })

}

And then call functionA() with whatever params you need to.

You call the function that includes async codes therefore getNumOfSessionForTrainerClientList doesn't depend the codes above (starts with(rows[i]["NUM_OF and console) what you should do is

  1. Make the function async by returning prom

  2. execute it with await keyword

    async function trainerClientList(){ return new Promise((resolve,reject)=>{ var getNumOfSessionForTrainerClientList = "select * from SCHEDULE WHERE CLIENT_USERNAME =? AND ASSIGNED_TRAINER =?" mysqlconn.connect(async function(err) { if (err) { console.error('Database connection failed: ' + err.stack); resolve() } mysqlconn.query(getNumOfSessionForTrainerClientList, [rows["USERNAME"], req.session.username], async function(err, sessionData) { if (err) { console.log(err); } else { numOfSessionsLeft.numberOfSessionsLeftIs = 1; console.log(numOfSessionsLeft.numberOfSessionsLeftIs) } }) }) resolve() })

    }

    for (var i = 0; i < rows.length; i++) { await trainerClientList() rows[i]["NUM_OF_SESSIONS_PER_MONTH"] = (parseInt(rows[i]["NUM_OF_SESSIONS_PER_MONTH"]) - numOfSessionsLeft.numberOfSessionsLeftIs) console.log(numOfSessionsLeft) }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM