0

I am new to NodeJs and I'm finding the Non Blocking and Asynchronous nature of JS extremely difficult to understand and handle,
I have a piece of code which is supposed to Iterate an array
and for every iteration, I'm supposed to make a DB update.

Can someone provide the correct implementation of Async library functions and help fix my code?

Code example -

function updateFunction(conn, requestBody, callback) {

    let arr = [];

    async.each(requestBody.arr, function(item, callback) {
        let sqlData = []
        let columns = "";

        if(item.columnData != null){
            sqlData.push(item.columnData);
            columns += "`columnName` = ?,";
        }

        if(columns != ''){
            columns = columns.substring(0,columns.length-1);

            let sqlQuery = 'UPDATE someTable SET '+columns
            +' WHERE id = "' + item.id + '"';

            conn.query(sqlQuery, sqlData, function (err, result) {
                if (err) {
                    return callback(err, false);
                }
            })
        }
        else{
            return callback(null, false);
        }
        columns = "";
        sqlData = [];
    },
    function(err, results) {
    //Code never reaches here, don't know why       
     if (err) {
            return callback(err, false);
        }
        else{
           return callback(null, true);
        }
    });
} // END 
2
  • I would go with ES6 async and await, or I would prefer using axios.. they are certainly easy and reliable Commented Feb 17, 2019 at 13:14
  • Can help with some code snippet? The conn.query part is becoming a nightmare in my code, not able to handle it. Commented Feb 17, 2019 at 13:15

2 Answers 2

1

During your database query call, on a successful query your callback is not called, therefore causing your code to never reach the final callback.

You will want to add another return statement after your if (err) { return callback(err); } to let async know your database query is finished.

And another thing, according to the docs, the async each method's final callback does not invoke with results in its callback.

A callback which is called when all iteratee functions have finished, or an error occurs. Invoked with (err).

Therefore, it is not required for you to pass a value into the callback statement within your iteratee function.

Modify your code to do this and it will work.

conn.query(sqlQuery, sqlData, function (err, result) {
    if (err) {
        return callback(err);
    }

    return callback(null);
})

Hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

Will adding the callback in successful DB update break the loops or just iterate onto the next element of the array?
Actually neither. The each method does not loop like a typical forEach loop for arrays. If you would like your array to loop sequentially you should use eachSeries method instead.
0
conn.query(sqlQuery, sqlData, async function (err, result) {
                if (err) {
                    return  await callback(err, false);
                }
            })

Something like this.. so the function callback is async here and we gave await which actually waits until the return call is finished..

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.