Steven Yong Steven Yong - 3 months ago 15
Javascript Question

Passing additional parameters to callback

I am using mssql in node.js executing an SP:

var fnGetMapping = function(results) {

new sql.Request(db.sqlConnection)
.input('myParam', sql.VarChar(60), 'myValue')
.execute('usp_MySP', fnProcessUUIDData);
};


Now my fnProcessUUIDData looks like this:

var fnProcessUUIDData = function(err, recordsets) {
if (err) {
console.log("Error calling MS SQL: " + err.message);
} else {
if (recordsets[0].length > 0) {

} else {

};
}
};


What I need now is to pass one of my own parameter into fnProcessUUIDData.

I read some articles that this is possible but I could get it working on my case, would appreciate some comments if what I want to do is possible.

Answer

You can use apply to pass some extra arguments without loosing the context.

new sql.Request(db.sqlConnection)
  .input('myParam', sql.VarChar(60), 'myValue')
  .execute('usp_MySP', function(...args) {
    fnProcessUUIDData.apply(this, args.concat(some, extra, parameters));
  });

var fnProcessUUIDData = function(err, recordsets, some, extra, parameters) {
  console.log(this, some, extra, parameters);
};