1

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 couldn't get it working on my case, would appreciate some comments if what I want to do is possible.

Community
  • 1
  • 1
Steven Yong
  • 5,163
  • 6
  • 35
  • 56

2 Answers2

3

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);
};
Lewis
  • 14,132
  • 12
  • 66
  • 87
2

Using apply overcomplicates things because fnProcessUUIDData is a plain old function and not a method (it doesn't use this internally). All you need to do is wrap it in another function:

new sql.Request(db.sqlConnection)
  .input('myParam', sql.VarChar(60), 'myValue')
  .execute('usp_MySP', function(err, recordsets) {
    fnProcessUUIDData(err, recordsets, additional_parameter);
  });
slebetman
  • 109,858
  • 19
  • 140
  • 171