如何从这个Twitter api调用中获取args

时间:2017-05-24 16:16:30

标签: javascript node.js twitter

我制作了一个Twitter机器人,它正在运行。但它是一堆嵌套逻辑,我想重构为函数。

我有这个Twitter API调用,我想返回reply参数,

T.get('trends/place', { id: '23424977' }, function(err, reply) {
  // THE WHOLE APP IS BASICALLY IN HERE
{

它不会让我把这个功能命名为

T.get('trends/place', { id: '23424977' }, function getTrends(err, reply) {
  // THE WHOLE APP IS BASICALLY IN HERE
{

我搞砸了其他一些想法,但没有运气。

整个机器人在这里https://glitch.com/edit/#!/trending-mishap?path=server.js

2 个答案:

答案 0 :(得分:0)

尽管我能理解这个问题,但问题在于你想要将回调中的代码分离成单独的函数。没关系,没有什么能阻止你这样做。

这是一个粗略的例子:

T.get('trends/place', { id: '23424977' }, getTrends);
function getTrends(err, reply) {
   if (err) {
       handleError(err);
       return;
   }
   doSomethingWith(reply);
}
function doSomthingWith(reply) {
   // ...
}

等等。

答案 1 :(得分:0)

将您的函数移出.get参数,然后在.get回调中调用它,并将回复传递给它。

var yourSpecialFunction = function(values) {
    // do things here
};
T.get('trends/place', { id: '23424977' }, function(err, reply) {
    if (err) {
        // handle the error
    } else {
        yourSpecialFunction(reply);
    }
}