2017-04-12 156 views
0

我得到錯誤,找不到函數。 我不明白..是因爲https.get中的回調嗎? 我想在http請求完成時調用回調函數。http請求中的Nodejs回調函數

const happy = ['happy']; 
    twitterService.twitter(happy, '1233,1233', '50', function (length) { 
     console.log(length + " bepedie boopdap"); 
    }); 

twitter.js

module.exports.twitter = function (hashtags, location, distance, callbackLength) { 

    if (hashtags.length !== 0) { 
     let hashURL = createHashtagUrl(hashtags); 

     const options = { 
      host: config.apiURL, 
      path: '/1.1/search/tweets.json?q=' + hashURL + '&geocode=' + location + ',' + distance + 'km&count=100', 
      method: 'GET', 
      headers: { 
       'Authorization': 'Bearer ' + config.base64Token 
      } 
     }; 

     https.get(options, function (res) { 
      let body = ''; 

      res.on('data', function (data) { 
       body += data; 
      }); 

      res.on('end', function() { 
       let jsonObj = JSON.parse(body); 
       let length = jsonObj.statuses.length; 
       callbackLength(length); // HERE its says .. is not a function 

      }) 
       .on('error', function (e) { 
        console.log("ERROR BEEP BEEP" + e); 
       }); 
     }); 
    } 
}; 

function createHashtagUrl(hashtags) { 

    let hashURL = config.hashtagUnicode + hashtags[0]; 

    for (let i = 1; i < hashtags.length; i++) 
     hashURL += '+OR+' + config.hashtagUnicode + hashtags[i]; 

    return hashURL; 
} 

任何人的想法?

+0

你肯定對參數你傳遞twitterService.twitter(快樂, '1233,1233', '50' – KornholioBeavis

+0

是,如果我(5)if語句之前調用callbackLength,它的工作原理量.. –

+0

這是你正在使用的_actual_代碼嗎? – robertklep

回答

0

不確定,但可能需要更改請求部分。

var req = https.get(options, function (res) { 
     let body = ''; 

     res.on('data', function (data) { 
      body += data; 
     });   
    }); 

req.on('end', function() { 
       let jsonObj = JSON.parse(body); 
       let length = jsonObj.statuses.length; 
       callbackLength(length); 
      }) 
       .on('error', function (e) { 
        console.log("ERROR BEEP BEEP" + e); 
       });`enter code here`