函数返回未定义的推文

时间:2016-03-20 13:37:01

标签: node.js twitter

我试图在给定预定义搜索条件的情况下返回推文,这些变量只是用作示例的占位符。问题是变量推文和配置文件在运行代码时返回'undefined'。我不确定为什么这是“对象对象”,任何帮助将不胜感激。

var count = 10,
    profile = 'manutd',
    date = '2011-11-11',
    keyword = 'man city',
    lan = 'en',
    search = keyword + " since:" + date + " lang:" + lan;

var tweets = getTweets();
var profiles = getProfile();
console.log('Started')

function handleTweets(err, data){
  if (err) {
    console.error('Get error', err)
  }  
  else {
    console.log('Get Tweets');
    console.log(tweets);
    console.log('Finished');
  }
}

function handleFriends(err, data){
  if (err) {
    console.error('Get error', err)
  }  
  else {
    console.log('Get Friends');
    console.log(profiles);
    console.log('Finished');
  }
}

function getTweets(){
  client.get('search/tweets', { q: search, count: count, from: profile },
              handleTweets)
}

function getProfile( callback ){
  client.get('friends/list', { screen_name: profile, count: count },
             handleFriends)
}

1 个答案:

答案 0 :(得分:2)

var count = 10,
profile = 'manutd',
date = '2011-11-11',
keyword = 'man city',
lan = 'en',
search = keyword + " since:" + date + " lang:" + lan;

console.log('Started')

function handleTweets(err, data){
  if (err) {
    console.error('Get error', err)
  } else {
    console.log('Get Tweets');
    console.log(data);
    console.log('Finished');
  } 
}

function handleFriends(err, data){
  if (err) {
    console.error('Get error', err)
  } else {
    console.log('Get Friends');
    console.log(data);
    console.log('Finished');
  }
}

function getTweets(){
  client.get('search/tweets', { q: search, count: count, from: profile },
          handleTweets)
}

function getProfile( callback ){
  client.get('friends/list', { screen_name: profile, count: count },
         handleFriends)
}

您正在访问未定义的属性tweets&回调函数中的profiles,这就是为什么你得到undefined而不是api返回的数据的原因;)