nodejs mssql return recordset

JiJiJiAR picture JiJiJiAR · Jun 29, 2015 · Viewed 15.4k times · Source

Im trying to past the recordset from mssql request.query like a return value. Following the code on https://www.npmjs.com/package/mssql is easy to make a a console output but when I try to asign the recordset to another variable doesnt work. What Im doing wrong?

Thanks.

Answer

Explosion Pills picture Explosion Pills · Jun 29, 2015

The query is run asynchronously. console.log actually runs before resultado = recordset[0].VehiCLASS completes, so it's not set.

You must synchronize any code that relies on asynchronous operations. You have to do this by using the callbacks:

resultado = recordset[0].VehiCLASS;
console.log("rsul: ", resultado);

You may also specify your own callback function to prevent nesting:

function queryComplete(err, result) {
    // should handle error

    console.log("rsul: ", result);
}

resultado = recordset[0].VehiCLASS;
queryComplete(null, resultado);