回调需要两个参数

时间:2017-12-28 05:08:09

标签: javascript node.js callback

我在node.js中设计了一个简单的Web应用程序,我们只是使用从index.jsrectangle.js模块的回调。但是,我收到了这个回调错误,并且不了解导致这种情况的语法: -

index.js

// importing rectangle node module
var rect = require('./rectangle')

function solveReact(l, b){
    console.log("l = "+l, "b = ", +b);

// We are using node module to call our callback,
// Note callback, always returns an erorr and function, and takes an error 
// and funciton as parameters
    rect(l, b, (err, rectangle)=> {
        if(err){
            console.log("Error:",err.message);
        }
        else{
            console.log("perimeter:"+ rectangle.perimeter(), "area:"+ 
            rectangle.area());
        }
    });

    // This is after call to react, but this will execute before our rect() 
    //function finishes, because of async calls
    console.log("After rect call")
};

// Some examples
solveReact(5, 6)
solveReact(-2, 3)

rectangle.js

// Using node style export
module.exports = (x, y, callback) => {
    if(x <= 0 || y <= 0){
        // simulating a database call error, using delay
        setTimeout(
            callback(new Error("length and width needs to be greater than zero"), 
            null),
            2000);
    }

    else{
        // simulating a successful database call, using delay
        setTimeout(
            callback(null, {
                perimeter: () => (2 * (x + y)),
                area : () => (x*y)
            }),
            2000);
    }
}

错误

l = 5 b =  6
perimeter:22 area:30
timers.js:427
    throw new TypeError('"callback" argument must be a function');
    ^

TypeError: "callback" argument must be a function
    at setTimeout (timers.js:427:11)
    at module.exports (C:\Users\ADMIN\Documents\coursera_server_side_programming
_with_node\simple_node_app\rectangle.js:26:9)
    at solveReact (C:\Users\ADMIN\Documents\coursera_server_side_programming_wit
h_node\simple_node_app\index.js:39:5)
    at Object.<anonymous> (C:\Users\ADMIN\Documents\coursera_server_side_program
ming_with_node\simple_node_app\index.js:54:1)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Function.Module.runMain (module.js:676:10)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! simple_node_app@1.0.0 start: `node index`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the simple_node_app@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional log
ging output above.

3 个答案:

答案 0 :(得分:1)

您只需调用callback并在 setTimeout 之后将该函数调用的结果(不是函数)设置为执行。那么为什么你会得到回调参数不是函数的错误。 setTimeout 中第一个参数的名称命名为callback,这会使您的函数名称混淆 - 错误与此参数有关。你需要在另一个函数中调用你的函数。此函数将在给定时间后调用,并且此时将调用您的回调

setTimeout(() => callback(new Error("length and width needs to be greater than zero"), null),
           2000);

在代码中使用其他 setTimeout -s执行相同的方法。

答案 1 :(得分:1)

setTimeout(()=>)

setTimeout需要一个函数。你给了它回调你的回调,这不是一个函数。

答案 2 :(得分:0)

您需要修改 setTimeOut 功能,在上面的代码中将返回的值从回调()传递给 setTimeout ,即不一个功能。将 setTimeout 两个函数更改为此

setTimeout(function() {
        callback(
            new Error('length and width needs to be greater than zero'),
            null,
        );
    }, 2000);

和另一个功能

setTimeout(function() {
        callback(null, {
            perimeter: () => 2 * (x + y),
            area: () => x * y,
        });
    }, 2000);

现在可行了

相关问题