Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
存储来自twitter api javascript(nodejs)的数据_Javascript_Node.js_Api_Twitter - Fatal编程技术网

存储来自twitter api javascript(nodejs)的数据

存储来自twitter api javascript(nodejs)的数据,javascript,node.js,api,twitter,Javascript,Node.js,Api,Twitter,我好像有点问题。我正在尝试从twitter api导出数据,但我不能。我的代码是这样的: /// Twitter.js module.exports = Twitter function Twitter(twitter) { this.twitter = twitter } Twitter.prototype.friends = function(params) { this.twitter.get('friends/list', params, (err, data) =&

我好像有点问题。我正在尝试从twitter api导出数据,但我不能。我的代码是这样的:

/// Twitter.js
module.exports = Twitter

function Twitter(twitter) {
    this.twitter = twitter
}

Twitter.prototype.friends = function(params) {
    this.twitter.get('friends/list', params, (err, data) => {
        if (err)
            console.log(err)

        console.log(data) // this prints data to the console but 
                          // I want to export and save in a data structure
                          // array or object
    }
}

///bot.js
var Twit = require('twit')
var config = require('/path_to_config.js')
var init = Twit(config)
var Twitter = require('/path_to_Twitter.js')

var bot = new Twitter(config)
bot.friends({ screen_name: 'myscreenname'})

如果我尝试返回数据参数,它将返回未定义的,如果我将data.users推送到数组,它将返回一个空数组。感谢您的帮助。

看起来可能是异步问题。我不熟悉Twitter API,但您可以尝试(或其变体)以下内容:

Twitter.prototype.friends = function(params, cb) {
  this.twitter.get('friends/list', params, cb);
};
然后:

var bot = new Twitter(config)
bot.friends({ screen_name: 'myscreenname'}, (err, data) => {
  // Do something with the data here.
  console.log('data', data);
});

这是相同的逻辑,但将异步响应移动到对您可能尝试执行的操作更有用的位置。

我建议使用承诺来允许调用代码处理返回的数据:

Twitter.prototype.friends = function(params) {
    return new Promise((resolve, reject) => {
        this.twitter.get('friends/list', params, (err, data) => {
           if (err) {
               reject(err);
           } else {
               resolve(data);
           }
        });
     });
}

...
var bot = new Twitter(config)
bot.friends({ screen_name: 'myscreenname'}).then((data) => {
    // do something with data
    console.log(data);
}).catch((err) => {
    console.error(err);
});

传递到
get
函数的第三个参数是。如果向回调内的数组添加值,并在调用回调之前尝试访问该数组,则该数组将为空/未定义

在我看来,你有两个选择:

  • 在你的回拨电话中调查你需要做的工作。可以直接调用,也可以将回调传递到函数中。编辑:查看上面@JohnnyCoder对后者的回答
  • 考虑使用一种新的方法。如果推特API不支持它,你可以考虑将回调变成一个承诺(通过包装结果)。有关如何使用本机JavaScript API将回调转换为承诺的相当简单的教程,请查看

  • 我更喜欢这个答案,而不是我的-承诺绝对是更好的方式。:-)@谢谢你!回调模式仍然是一个非常好的选择。:)哈哈,当你发布时,我正在使用这两个建议键入回复!:这是一个异步问题。谢谢:-)