1

I'm writing an application in Node, for which I've got a function which I extend with some methods.

function WsClient(key, secret, url) {
    // here some init code
}
WsClient.prototype.addLisener = function(id, listener) {
    // code
}
WsClient.prototype.request = function(action, data) {
    // code
};
WsClient.prototype.getpositions = function() {
    this.request('/api/v1/positions', {});
};

let wsclient = new WsClient(KEY, SECRET);
wsclient.getpositions();
setInterval(() => {console.log('CALLING FROM INSIDE INTERVAL'); wsclient.getpositions()}, 1111);

But to my surprise I now get a TypeError in the getpositions() method, saying this.request is not a function. The code looks good to me, but coming from Python/php I'm unsure what I'm doing wrong here.

Does anybody know what mistake I'm making here?

[EDIT]

I just found out that it is successfully called multiple times before it fails with this error. Furthermore, it is successfully called from various other methods as well, which first succeed, and finally it fails on one of the calls with this error. I'm unsure why this happens. Any idea?

kramer65
  • 50,427
  • 120
  • 308
  • 488
  • 1
    Is this actually the way you’re calling `getpositions`, or is it a “simplified” version? – Ry- Oct 26 '17 at 03:01
  • @Ryan - Well, I call it once at the start, and then using a setInterval. I just found out that it is successfully called several times before it fails. I added this information to the question. – kramer65 Oct 26 '17 at 03:10
  • @Ryan - Ok this is really weird. I changed the interval call from `setInterval(wsclient.getpositions, 1111);` to `setInterval(() => {console.log('CALLING FROM INSIDE INTERVAL'); wsclient.getpositions()}, 1111);` so that I could include the console.log, but to my surprise, that also solves the problem. Any idea why that could be? – kramer65 Oct 26 '17 at 03:31
  • You should work in [strict mode](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode); it gives more useful error messages and behaviour. – Ry- Oct 26 '17 at 03:40
  • @Ryan - I work in strict mode already. I do get a stacktrace, but I don't think it tells me anything meaningful. Or would a stacktrace help you? – kramer65 Oct 26 '17 at 08:20
  • Oh, never mind then. Node’s `setInterval` sets `this`, as it turns out. Anyway, that was just side advice – see the linked duplicate for the cause of and solution to your problem. – Ry- Oct 26 '17 at 15:52

0 Answers0