Q:

at node-mssql return the query result

function getAllCustomers() {

    const pool = new sql.ConnectionPool(config);
    const request = new sql.Request(pool);

    try {
        pool.connect(function () {
            request.query('select * from Customers', function (err, res) {
                console.log(res.recordset);
                return res.recordset;
            });
        });
    } catch (error) {
        console.log(error);
    }
    return;
}

I smoothly printing res.recordset to the console but when I want to return this value from the function, returns null. What should I do for this?

A:

you are getting response of request.query function when a callback is being called. Your last return will execute before the callback is called that is why you are getting null.
function getAllCustomers(callBack) {

const pool = new sql.ConnectionPool(config);
const request = new sql.Request(pool);

try {
    pool.connect(function () {
        request.query('select * from Customers', function (err, res) {
            if(err){
                callBack(err);
            }
            else{
                console.log(res.recordset);
                callBack(false,res);
            }
        });
    });
} catch (error) {
    console.log(error);
    callBack(error);
}}

