firebird
firebird

Reputation: 3511

How to pass arguments into function?

I have the following function in nodejs, res.execSync takes in multiple parameters as detailed here: https://github.com/xdenser/node-firebird-libfbclient

function execSync(param1, param2, ..., paramN);
param1, param2, ..., paramN - parameters of prepared statement in the same order as in SQL and with appropriate types.

function test(sql, callback)
{
  var args = Array.prototype.slice.call(arguments).splice(2);
  res.execSync(args);
}

test('test', function() {}, "param1", "param2", "param3");

Error: Expecting String as argument #1.

How do I resolve this?

Upvotes: 2

Views: 702

Answers (2)

hugomg
hugomg

Reputation: 69924

args is an array. You need to use the apply method to unpack it into separate arguments.

res.execSync.apply(res, args);

It works just like call but receiving an array intead of the usual arguments list.


BTW, you can pass range arguments to slice. This means that there is a shorter way to write your first line:

var args = Array.prototype.slice.call(arguments, 2);

Upvotes: 1

Ahmed Masud
Ahmed Masud

Reputation: 22374

Don't you mean:

function test(sql, callback)
{
  var args = Array.prototype.slice.call(arguments, 2);
  res.execSync.apply(res, args);
}

test('test', function() {}, "param1", "param2", "param3");

Upvotes: 1

Related Questions