简体   繁体   English

node.js restler调用的多个响应

[英]Multiple responses on node.js restler call

I've created a library for sending REST requests: 我已经创建了一个用于发送REST请求的库:

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.... 问题是每次我使用库并且响应调用时,'on complete'被多次调用:第一次使用方法将仅调用'on complete'一次,第二次使用方法将调用'on complete'两次等等....

What Am I doing wrong? 我究竟做错了什么?

Thanks Jose 谢谢何塞

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. 这是由每次调用rest.get() 注册的“完整”事件引起的。

My solution is to use .once() instead of .on(). 我的解决方案是使用.once()而不是.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. 希望这会有所帮助。

TL;DR: Bug in restler, quick fix until npm is updated: add git master to package.json TL; DR:restler中的bug,快速修复直到更新npm:将git master添加到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 . 这里真正的问题是节点0.10中对事件API的一些更改导致restler重新引用旧事件侦听器,如https://github.com/danwrong/restler/issues/112中所述

End of august this was fixed in https://github.com/danwrong/restler/pull/113 . 8月底这已在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. 当我们等待正确的npm发布时,它通过使用当前的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. 更新2014年8月:从那以后有一个npm版本,它似乎包括修复。

This is because you attach a new event for each call. 这是因为您为每个呼叫附加了一个新事件。 Try to unbind event first. 尝试先取消绑定事件。

An exception occurring in your callback handler for a JSON request can also cause this behaviour. JSON请求的回调处理程序中发生的异常也可能导致此行为。

See the pull request here for a solution for that: https://github.com/danwrong/restler/pull/94 请参阅此处的pull请求以获取解决方案: https//github.com/danwrong/restler/pull/94

Please check out v3.2.2. 请查看v3.2.2。 Upgrade your package.json: 升级你的package.json:

npm install restler --save

It solved this issue for me. 它为我解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM