Nodejs停止流时Twit模块错误

时间:2013-10-22 12:40:23

标签: javascript node.js angularjs streaming

我正在使用Twit module在AngularJS应用中显示Twitter流。我想在用户单击停止按钮时提供关闭流的选项。

处理流媒体的脚本:

var Twit = require('twit');
var T = new Twit({
    consumer_key        : 'XXX',
    consumer_secret     : 'XXX',
    access_token        : 'XXX',
    access_token_secret : 'XXX'
});

function streamingHashtag(streamBool)
{    
  var stream = T.stream('statuses/filter', { track: 'test' });

  stream.on('tweet', function (tweet) {
    console.log(tweet);
  });

  stream.on('disconnect',function(disconnectMessage){
    stream.stop();
  });

  if(streamBool == false){
    stream.stop();
    console.log('Stream stopped by user');
  }
}

按下按钮后停止控制台输出以下错误,然后流继续:

message: "Cannot call method 'abort' of undefined", stack: "TypeError: Cannot call method 'abort' of undefined…es\connect\lib\middleware\methodOverride.js:37:5)"}
message: "Cannot call method 'abort' of undefined"
stack: "TypeError: Cannot call method 'abort' of undefined?    at OARequest.stop ([localhost]\node_modules\twit\lib\oarequest.js:110:16)?    at Object.streamingHashtag ([localhost]\server\library\twitter.js:40:10)?    at exports.twitterStream ([localhost]\server\routes\index.js:11:10)?    at callbacks ([localhost]\node_modules\express\lib\router\index.js:161:37)?    at param ([localhost]\node_modules\express\lib\router\index.js:135:11)?    at pass ([localhost]\node_modules\express\lib\router\index.js:142:5)?    at Router._dispatch ([localhost]\node_modules\express\lib\router\index.js:170:5)?    at Object.router ([localhost]\node_modules\express\lib\router\index.js:33:10)?    at next ([localhost]\node_modules\express\node_modules\connect\lib\proto.js:190:15)?    at Object.methodOverride [as handle] ([localhost]\node_modules\express\node_modules\connect\lib\middleware\methodOverride.js:37:5)"

我正在运行最新版本(package.json说“版本”:“1.1.11”)并且在调查时我发现OARequest.js中的stop函数指向abort()。

我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

对于任何得到相同错误并供将来参考的人,让我回答我自己的问题,因为我找到了答案。错误表明在我确定我声明了变量时,未定义的方法“中止”。

错误是由两次调用函数引起的,创建了一个新的作用域,其中旧的变量不可用。在函数外部定义变量全局修复此问题。