pool.request().query(sqlQuery) RowsAffected

Viewed 582

I need some insight on how the pool.request().query(sqlQuery) works.

I have the code working fine and all, but I need to learn what all I can do with the result of the query.

My current code within a function:

const result = await pool.request().query(sqlQuery);
return result;

In this code, when I run the debugger and inspect the value of result, I see

output:Object {}
recordset:Array(1) [Object]
recordsets:Array(1) [Array(1)]
rowsAffected:Array(0) []
__proto__:Object {constructor: , __defineGetter__: , __defineSetter__: , …}

So I was assuming that the I can use these. I went ahead and used

  const result = await pool.request().query(sqlQuery);
      logger.info(" rows affected "+ rowsAffected.Array(0));
// The above is where I am not sure how I can get the rows affected.
     return result;

I don't seem to be winning here, as the console log says

rows affected undefined.

I obviously don't know my ways here and there is probably something I am missing. I checked the google but all the suggestions are resulting in undefined.

I need the rowsAffected information to pass the right message beck to the response. How do I get the rowsAffected value

What would I be missing?

1 Answers

rowsAffected:Array(0) []

means that result has a property named rowsAffected and it's type is an Array. This array happens to have 0 elements.

Change your logger statement to

logger.info(" rows affected "+ result.rowsAffected[0]);

This is assuming that sqlQuery is actually adding or updating records. The fact that you have a 0-element array implies you are not updating any records in this call.

Related