2017-12-28 35 views
0

나는 index.js의 콜백을 rectangle.js 모듈에 사용하는 node.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! [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. 

답변

0

당신은 당신이 콜백()에서는 setTimeout 에 즉하지 함수에서 반환 된 값을 전달 위의 코드에서, 당신의 의 setTimeout 기능을 편집해야합니다. 이

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

다른 함수에 에서는 setTimeout 모두에게 기능을 변경

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

지금은

1

방금 ​​callback을 호출하고 setTimeout 뒤에 이 실행 된이되도록 함수가 아닌 해당 함수 호출의 결과를 설정하면됩니다. 그렇다면 콜백 인수은 함수가 아닙니다. setTimeout에있는 첫 번째 매개 변수의 이름은 callback이며 함수 이름과 혼동을줍니다.이 오류는이 매개 변수와 관련이 있습니다. 다른 함수 안에서 함수를 호출해야합니다. 이 기능은 주어진 시간 후에 호출되며 콜백은 코드에서 다른 에서는 setTimeout -s이 동일한 방법을 수행하는 시간

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

에서 호출됩니다.

1

setTimeout(()=>)

의 setTimeout 함수를 취 작동합니다. 콜백을 돌려주었습니다. 콜백은 함수가 아닙니다.