Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 节点回调-为什么不起作用?_Javascript_Node.js_Callback - Fatal编程技术网

Javascript 节点回调-为什么不起作用?

Javascript 节点回调-为什么不起作用?,javascript,node.js,callback,Javascript,Node.js,Callback,我目前正在尝试理解节点和回调,并尝试了该代码的各种解决方案以使其工作,但是param2返回时未定义。有人能告诉我为什么吗?如何解决这个问题?谢谢 function getPage(callback) { url = 'http://www.google.com'; if (url) { url = url; } else { console.log('There was an error. No URL submitted'); } callback(u

我目前正在尝试理解节点和回调,并尝试了该代码的各种解决方案以使其工作,但是param2返回时未定义。有人能告诉我为什么吗?如何解决这个问题?谢谢

function getPage(callback) {

  url = 'http://www.google.com';

  if (url) {
    url = url;
  } else {
    console.log('There was an error. No URL submitted');
  }

  callback(url, param2);
}

function CB(url, param2) {
console.log(`The URL of the page requested was ${url} and the added argument          was ${param2}`);
}

 getPage(CB);

在getPage函数中,定义param2并传递它。在getPage函数中,定义param2并传递它,这是因为在调用回调函数的范围中,
param2
未定义。要定义
param2
返回,请确保在
getpage()中初始化
param2


这是因为在调用回调函数的范围中,
param2
未定义。要定义
param2
返回,请确保在
getpage()中初始化
param2


谢谢我现在觉得有点傻。我早该知道的!无论如何,再次谢谢你!谢谢我现在觉得有点傻。我早该知道的!无论如何,再次谢谢你!
function getPage(callback) {

  url = 'http://www.google.com';
  // **make sure param2 is defined**
  var param2 = "param2 value"

  if (url) {
    url = url;
  } else {
    console.log('There was an error. No URL submitted');
  }

  callback(url, param2);
}

function CB(url, param2) {
console.log(`The URL of the page requested was ${url} and the added argument was ${param2}`);
}

 getPage(CB);