Javascript 回调不是函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33132338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:40:27  来源:igfitidea点击:

Callback is not a function

javascriptnode.js

提问by Peter

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.

我正在尝试检索一个 json 对象以在另一个模块中使用它,但是我遇到了回调问题。我有错误“回调不是函数”。我使用回调是因为我的变量描述未定义,所以我猜这是异步的问题。

Could you help me plz :)

你能帮我吗 :)

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

    http.get("http://www.website.com", 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);

回答by

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.com", 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);
});

回答by skypHyman

The method http.getrequires 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.

该方法http.get需要一个函数,该函数只接受一个命名参数response(或任何你想要的参数,名称实际上并不重要),因此你的第二个,callback, 是未定义的,调用它会以该错误结束。