9

I've created a library for sending REST requests:

var rest = require('restler');
module.exports = {
  beginSession: function()
  {
    var options = {
        method: "GET",
        query: {begin_session: '1'}};
    rest.get('http://countly/i', options).
        on('complete', function(data, response){
            console.log('Status: ' + response.statusCode);
        });
  }
};

The problem is that every time I use the library and the call is responded, the 'on complete' is called multiple times: 1st use of method will call 'on complete' just once, 2nd use of method will call 'on complete' twice and so on....

What Am I doing wrong?

Thanks Jose

JoseOlcese
  • 532
  • 6
  • 14

5 Answers5

14

I was struggling with this one as well. but didn't find an answer on the internet. I finally figure it out though. It was caused by the 'complete' event being registered every time the your rest.get() is called.

My solution is to use .once() instead of .on(). For example:

var rest = require('restler');
rest.get('url_to_fetch').once('complete', function(rtn, rsp){
     ....blah blah....
});
// refer here http://nodejs.org/api/events.html#events_emitter_once_event_listener

Hopefully this helps.

user2413287
  • 141
  • 2
4

TL;DR: Bug in restler, quick fix until npm is updated: add git master to package.json

The real problem here is that some changes to the event API in node 0.10 results in restler refiring old event listeners as described in https://github.com/danwrong/restler/issues/112.

End of august this was fixed in https://github.com/danwrong/restler/pull/113. While we wait for a proper npm release it works for me by using the current git head.

"restler": "git://github.com/danwrong/restler.git#9d455ff14c57ddbe263dbbcd0289d76413bfe07d"

DISCLAIMER: I don't know what is broken in this version or why it is not released yet. I did not go trough the issues or diffs since last release to find out.

UPDATE Aug 2014: There was a npm release since then, it seems to include the fix.

beilharz
  • 41
  • 5
2

This is because you attach a new event for each call. Try to unbind event first.

Aleko
  • 960
  • 1
  • 9
  • 10
  • Thanks Aleko!. I've implemented your suggestion by removing the Listener on the callback function: `var restCall = rest.get(countlyUrl , options).on('complete', callback); function callback(data, response) {console.log('Status: ' + response.statusCode);restCall.removeListener('complete', callback);};`. But now I have a doubt on how to match each request with each callback. What if I have different callbacks for simultanous requests? – JoseOlcese Apr 17 '13 at 01:10
1

An exception occurring in your callback handler for a JSON request can also cause this behaviour.

See the pull request here for a solution for that: https://github.com/danwrong/restler/pull/94

Josh Wulf
  • 4,727
  • 2
  • 20
  • 34
1

Please check out v3.2.2. Upgrade your package.json:

npm install restler --save

It solved this issue for me.

Igor Escobar
  • 1,047
  • 1
  • 12
  • 13
  • 1
    @arco444 what exactly do you want me to say? I said what he needs to listen! It's a bug! upgrade your npm packge to v3.2.2 and you will be just fine! – Igor Escobar Nov 19 '14 at 16:04
  • I'm saying this because my code is pretty much identical of the author of this issue and upgrading the restler version solved the issue for me. – Igor Escobar Nov 19 '14 at 16:06
  • @arco444 I edited my original answer just for you :* – Igor Escobar Nov 19 '14 at 16:09
  • Oh god... low quality according to who? you? I have 100% of sure that the person who are having issues will appreciate my answer. – Igor Escobar Nov 19 '14 at 16:33
  • 1
    I don't know what discussion just went on but Igor is correct in that it was a terrible Restler bug. I spent many hours debugging this. Upgrading from 2.0.1 to 3.2.2 fixed the memory leaks and multiple calls I was experiencing. – Mauvis Ledford Feb 19 '15 at 23:29