0

我正在使用Twit 模块在 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 中的停止函数指向 abort()。

我在这里做错了什么?

4

1 回答 1

0

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

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

于 2013-10-23T13:27:37.647 回答