无法成功地将回调传递到功能链中

时间:2016-05-05 11:09:11

标签: javascript node.js express callback

我使用codebird库来执行对Twitter API的一些请求。这些请求会正确响应,但我想将该响应传递给我的路由。这是我的route.js的片段:

router.get('/twitter', function(req, res) {

  twitterAPI.callAPI(function(tweetData) {
    console.log('callback for twitterCall');
    res.send(tweetData);
  });

});

这是我的module1.js代码:

require('es6-promise').polyfill();
require('isomorphic-fetch');
var Codebird = require("codebird");

// using twitter-aggregation app for consumer key and secret
var params = {
  screen_name: 'kanyewest'
};

var config = {
  twitter : {
    consumerKey: 'thisismykey',
    consumerSecret: 'thisismysecret'
  }
};

var cb = new Codebird;
cb.setConsumerKey(config.twitter.consumerKey, config.twitter.consumerSecret);

var tweetData = {};

// Function to call twitter api, called by route
var callAPI = function(callback) {

  getAccessToken(callback());

  console.log('callAPI function loaded');
};

var getAccessToken = function(callback) {
  cb.__call(
    "oauth2_token",
    {},
    function (reply, err) {
      var accessToken;
      console.log('1. response received');
      if (err) {
          console.log("error response or timeout exceeded" + err.error);
          return;
      }
      if (reply) {
          console.log('2. twitter: reply received');
          console.log(reply);
          accessToken = reply.access_token;
          getUserTweets(accessToken, callback);
      }
    }
  );
};

var getUserTweets = function(authToken, callback) {
  console.log('passed accessToken');
  console.log(authToken);
  console.log(typeof callback);
  cb.__call(
    "users_show",
    params,
    function (reply, rate, err) {
      if (err) {
        console.log("Error in showing user or timed out" + err.error);
      }
      if (reply) {
        console.log("received user's tweet");
        var newTweetData = {
          screen_name: reply.screen_name,
          name: reply.name,
          status_text: reply.status.text,
          status_date: reply.status.created_at,
          status_retweets: reply.status.retweet_count,
          status_favourites: reply.status.favorite_count
        };
        console.log(newTweetData);
        // final callback to set a variable
        console.log(typeof callback);
        setTweetData(newTweetData, callback);
        // console.log('getUserTweets callback');
        // callback;

      }
    }
  );
};

var setTweetData = function(newTweetData, callback) {
  if(!newTweetData) {
    return 'variable is a string, not function';
  }

  tweetData = newTweetData;
  console.log('tweet data has been set');
  console.log(callback);
  callback(tweetData);
};




module.exports = { params, callAPI };

不确定将回调从我的路线传递到我的功能链的末尾是什么方法,这样一旦我获得了推特数据,我的res.send()就会触发。

1 个答案:

答案 0 :(得分:1)

您必须更改callApi方法以将“callback”作为参数传递,而不是调用它。

var callAPI = function(callback) {

  getAccessToken(callback);
  // and not getAccessToken(callback());

  console.log('callAPI function loaded');
};

不同之处在于,在您的代码中,您只是在身份验证之后调用回调。

更改后,您将回调传递给 getUserTweets 函数,并在获取推文后调用回调。