node.js - 返回未定义推文的函数

标签 node.js twitter

我试图返回给定预定义搜索条件的推文,变量仅用作示例的占位符。问题在于,当代码运行时,变量 tweets 和 profile 返回“未定义”。我不确定为什么这是“对象对象”,任何帮助将不胜感激。

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)
}

最佳答案

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)
}

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

关于node.js - 返回未定义推文的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36114416/

相关文章:

node.js - Async.times 是否并行运行?

node.js - 以编程方式重新启动 Node.js 服务器

windows - nodejs- 在 Windows 8 中使用 npm 时启动错误 : ENOENT,

python - 为什么我不能使用 tweepy 搜索 API 访问超过 25 条推文?

twitter - Tweepy 获取用户创建 Twitter 帐户的日期

Flash 安全错误 - Twitter API

node.js - 如何在 Node.js 中最好地实现套接字客户端并中继到前端?

node.js - 为什么zombie.js浏览器在使用Bootstrap时返回空HTML?

android - 使用 Volley POST 请求获取 Twitter 授权 token

android - 在不使用应用程序后台 Intent 的情况下在 Twitter 上共享文本