1

Hi I am trying to get a response via a http using the callback method. But I get an error saying callback is not a function.

 module.exports.ipLookup = function (res, callback) {
    var http = require('http');
    var str = '';
    var options = {
        host: 'ip-api.com',
        port: 80,
        path: '/json/',
        method: 'POST'
    };
    var str= "";
    var req = http.request(options, function (res) {
        res.on('data', function (body) {
            str += body;
        });

        res.on('end', function () {
            callback(str);
        });
    });


    req.end();

    return str;

    }

What is should to id return the json api response via ip-api.com. If anyone can help me on this it would be greatly appreciated.

2
  • 1
    How are you using/invoking ipLookup()? The value of callback will be determined there. Commented Nov 22, 2015 at 5:55
  • @JonathanLonowski, thanks for the reply. I am requiring the js file onto another file. e.g. var lookup = require('ip'); using lookup.ip(); Commented Nov 22, 2015 at 8:35

1 Answer 1

4

In the other file you are loading the function from you need to ensure the callback parameter is used. See snippet below

var Lookup = require('./ip');
Lookup.ipLookup(function (response) {

    console.log(response) // check if response is valid
})

You need to change the exported function to only accept one parameter as the first parameter is not used inside function body like so.

module.exports.ipLookup = function (callback) {

    var http = require('http');
    var str = '';
    var options = {
        host: 'ip-api.com',
        port: 80,
        path: '/json/',
        method: 'POST'
    };

    var req = http.request(options, function (res) {

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

         res.on('end', function () {
             return callback(str);
         });
    });
    req.end();

}
Sign up to request clarification or add additional context in comments.

4 Comments

thanks for this worked like a charm, one more question how do I make the "response" variable accessible out of the variable scope? Hope you can help me out on this. thanks
Either hoist a variable which can be shared by other functions or exported to another module to be used. Or chain as part of a series of task and work on objects to achieve what you want to do? What do you want to do with the context?
what I want to do is in this case use the "response" as an array and break down the values and assign them to separately. Again thanks for the help
Call the function with a control flow library and merge return values into your requirement. I use neo-async as haven't made switch to native promises yet!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.