javascript - Callback is not a function - Stack Overflow

I am trying to retrieve a json object to use it in another module, but I have a problem with callback.

I am trying to retrieve a json object to use it in another module, but I have a problem with callback. I have the error "callback is not a function". I use callback because my variable description is undefined, so i guess it's a problem of asynchronous.

Could you help me plz :)

var leboncoin = function () {
    var http = require('http')
    var bl = require('bl')

    http.get("", function (response, callback) {
        response.pipe(bl(function (err, data) {
            if (err) {
                return console.error(err)
                callback(err);
            }
            var data = data.toString()

            var brand = ...
            var model = ...
            var releaseDate = ...
            var km = ...
            var fuel = ...
            var gearbox = ...

            description.Brand = brand;
            description.Model = model;
            description.Year = releaseDate;
            description.KM = km;
            description.Fuel = fuel;
            description.Gearbox = gearbox;

            callback(description);
            return (description)

            /*console.log(description.Brand);
            console.log(description.Model);
            console.log(description.Year);
            console.log(description.KM);
            console.log(description.Fuel);
            console.log(description.Gearbox);*/

        }))
    })
}

exports.leboncoin = leboncoin;

var module = require('./leboncoin');
var res = module.leboncoin();

console.log(res);

I am trying to retrieve a json object to use it in another module, but I have a problem with callback. I have the error "callback is not a function". I use callback because my variable description is undefined, so i guess it's a problem of asynchronous.

Could you help me plz :)

var leboncoin = function () {
    var http = require('http')
    var bl = require('bl')

    http.get("http://www.website.", function (response, callback) {
        response.pipe(bl(function (err, data) {
            if (err) {
                return console.error(err)
                callback(err);
            }
            var data = data.toString()

            var brand = ...
            var model = ...
            var releaseDate = ...
            var km = ...
            var fuel = ...
            var gearbox = ...

            description.Brand = brand;
            description.Model = model;
            description.Year = releaseDate;
            description.KM = km;
            description.Fuel = fuel;
            description.Gearbox = gearbox;

            callback(description);
            return (description)

            /*console.log(description.Brand);
            console.log(description.Model);
            console.log(description.Year);
            console.log(description.KM);
            console.log(description.Fuel);
            console.log(description.Gearbox);*/

        }))
    })
}

exports.leboncoin = leboncoin;

var module = require('./leboncoin');
var res = module.leboncoin();

console.log(res);
Share Improve this question edited Oct 14, 2015 at 18:06 fuyushimoya 9,8133 gold badges29 silver badges34 bronze badges asked Oct 14, 2015 at 17:55 PeterPeter 931 gold badge1 silver badge10 bronze badges 4
  • where does said error happen within the given code? – Kevin B Commented Oct 14, 2015 at 17:58
  • 4 nodejs/api/http.html#http_event_response, it doesn't have the 2nd param – fuyushimoya Commented Oct 14, 2015 at 17:59
  • The error is at the line "callback (description)" – Peter Commented Oct 14, 2015 at 18:02
  • The callback function is undefined... – Danny Fardy Jhonston Bermúdez Commented Oct 14, 2015 at 18:03
Add a ment  | 

2 Answers 2

Reset to default 9

Callbacks aren't magic that just appear. You need to define a parameter to your function and pass the callback you want to use.

// --------------------------v
var leboncoin = function (callback) {
    var http = require('http')
    var bl = require('bl')

    http.get("http://www.website.", function (response) {
        response.pipe(bl(function (err, data) {
            if (err) {
                callback(err);
                return;
            }
            var data = data.toString()
            var description = { /* your description object */ }

            callback(description);
        }))
    })
}

exports.leboncoin = leboncoin;

var module = require('./leboncoin');

// -----------------vvvvvvvv
module.leboncoin(function(res) {
    console.log(res);
});

The method http.get requires a function that accepts only a parameter named response (or whatever you want, the name doesn't matter indeed), thus your second one, callback, is undefined and invoking it will end ever in that error.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743660886a4486175.html

相关推荐

  • javascript - Callback is not a function - Stack Overflow

    I am trying to retrieve a json object to use it in another module, but I have a problem with callback.

    7天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信