node.js restler调用的多个响应 [英] Multiple responses on node.js restler call
问题描述
我已经创建了一个用于发送REST请求的库:
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);
});
}
};
问题在于,每次我使用图书馆并且电话都被响应时,'on complete'多次调用:第一次使用方法只会调用'on complete'一次,第二次使用方法会调用'on complete'两次等等......
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....
我做错了什么?
谢谢
Jose
Thanks Jose
推荐答案
我也在努力解决这个问题。但没有在互联网上找到答案。我终于明白了。这是因为每次调用你的rest.get()时都会注册的完整事件。
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.
我的解决方案是使用.once()而不是.on()。例如:
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
希望这会有所帮助。
这篇关于node.js restler调用的多个响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!