Wednesday, 12 April 2017

Nodejs callback function in http request

I get the error that function is not found. I don't get it.. is it because of the callback in the https.get? I want to call the callback function when the http request is done.

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

anyone an idea?



via Jan Pecquet

No comments:

Post a Comment