I want to use the JavaScript .apply method to functions of a thrift piled for Node.js. The thrift .js file has code like this:
...
var NimbusClient = exports.Client = function(output, pClass) {
this.output = output;
this.pClass = pClass;
this.seqid = 0;
this._reqs = {};
};
NimbusClient.prototype = {};
NimbusClient.prototype.getClusterInfo = function(callback) {
this.seqid += 1; // line where error is thrown [0]
this._reqs[this.seqid] = callback;
this.send_getClusterInfo();
};
...
My server file looks the following way:
var thrift = require('thrift')
, nimbus = require('./Nimbus')
, connection = thrift.createConnection('127.0.0.1', 6627)
, client = thrift.createClient(nimbus, connection)
, ... // server initiation etc
app.get('/nimbus/:mand', function(req, res, next) {
client[req.paramsmand](console.log); // direct call [1]
client[req.paramsmand].apply(this, [console.log]); // apply call [2]
});
...
The direct call [1] returns the values as expected, but the apply call [2] always produces the following error in line [0]:
TypeError: Cannot set property 'NaN' of undefined
I tried several other scope parameters in [2]: null
, nimbus
, nimbus.Client
, nimbus.Client.prototype
, nimbus.Client.prototype[req.paramsmand]
and client[req.paramsmand]
, all without success.
How can I call the apply method without changing the actual scope of the function called, so that it behaves exactly the same as it would if called in the direct way?
I want to use the JavaScript .apply method to functions of a thrift piled for Node.js. The thrift .js file has code like this:
...
var NimbusClient = exports.Client = function(output, pClass) {
this.output = output;
this.pClass = pClass;
this.seqid = 0;
this._reqs = {};
};
NimbusClient.prototype = {};
NimbusClient.prototype.getClusterInfo = function(callback) {
this.seqid += 1; // line where error is thrown [0]
this._reqs[this.seqid] = callback;
this.send_getClusterInfo();
};
...
My server file looks the following way:
var thrift = require('thrift')
, nimbus = require('./Nimbus')
, connection = thrift.createConnection('127.0.0.1', 6627)
, client = thrift.createClient(nimbus, connection)
, ... // server initiation etc
app.get('/nimbus/:mand', function(req, res, next) {
client[req.params.mand](console.log); // direct call [1]
client[req.params.mand].apply(this, [console.log]); // apply call [2]
});
...
The direct call [1] returns the values as expected, but the apply call [2] always produces the following error in line [0]:
TypeError: Cannot set property 'NaN' of undefined
I tried several other scope parameters in [2]: null
, nimbus
, nimbus.Client
, nimbus.Client.prototype
, nimbus.Client.prototype[req.params.mand]
and client[req.params.mand]
, all without success.
How can I call the apply method without changing the actual scope of the function called, so that it behaves exactly the same as it would if called in the direct way?
Share Improve this question edited Dec 5, 2012 at 22:09 Thomas asked Dec 5, 2012 at 18:25 ThomasThomas 10.7k13 gold badges43 silver badges56 bronze badges 7- foo.apply(null,…) produces the same error – Thomas Commented Dec 5, 2012 at 18:30
-
I'm kind of curious as to why you want to use the
apply
method in the first place if you wish to retain the default scope... – subhaze Commented Dec 5, 2012 at 18:31 - @subhaze: I boiled it down for this example. I need the number of arguments to be variable, because most Nimbus functions expect more than just the callback as parameters. – Thomas Commented Dec 5, 2012 at 18:33
-
2
Ahh, ok. Have you tried
client[req.params.mand].apply(client, [console.log]);
by chance? – subhaze Commented Dec 5, 2012 at 18:34 - 1 this may be related stackoverflow./questions/11536177/… – lostsource Commented Dec 5, 2012 at 18:42
1 Answer
Reset to default 12Pointing the apply
function back to the same function like this should retain the original scope.
client[req.params.mand].apply(client, [console.log]);