Reputation: 562
I am designing a simple web app in node.js, where we are simply using callback from index.js
to the rectangle.js
module. But, I am getting this callback error, and don't understand which syntax is leading to this:-
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);
}
}
Error
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! [email protected] start: `node index`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional log
ging output above.
Upvotes: -1
Views: 298
Reputation: 4388
You need to edit your setTimeOut function , in your code above you pass the returned value from the callback() to setTimeout i.e. not a function . Change the both setTimeout functions to this
setTimeout(function() {
callback(
new Error('length and width needs to be greater than zero'),
null,
);
}, 2000);
and the other function
setTimeout(function() {
callback(null, {
perimeter: () => 2 * (x + y),
area: () => x * y,
});
}, 2000);
now it works
Upvotes: 0
Reputation: 469
setTimeout(()=>)
setTimeout takes a function. You gave it the return of your callback, which isnt a function.
Upvotes: 1
Reputation: 68675
You just call the callback
and set the result of that function call, which is not a function, to be executed after setTimeout. So why you get the error that callback argument is not a function. The name of the first parameter in the setTimeout is named callback
, which confuse you with your function name - the error is related to this parameter. You need to call your function inside another function. This function will be called after the given time and your callback will be called at that time
setTimeout(() => callback(new Error("length and width needs to be greater than zero"), null),
2000);
Do this same approach with other setTimeout-s in your code.
Upvotes: 1