Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.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
Node.js nodejs回调问题。。不是功能_Node.js - Fatal编程技术网

Node.js nodejs回调问题。。不是功能

Node.js nodejs回调问题。。不是功能,node.js,Node.js,我目前正在学习NodeJS,正在开发一个迷你应用程序,试图理解回调。然而,我不断得到错误: callback(undefined,price); ^TypeError: callback is not a function 这是我的代码: var getCoin = (coin, callback) => { request({url:`https://https://rest.coinapi.io/${coin}&ssr=USD`, json: tr

我目前正在学习NodeJS,正在开发一个迷你应用程序,试图理解回调。然而,我不断得到错误:

  callback(undefined,price);
      ^TypeError: callback is not a function
这是我的代码:

var getCoin = (coin, callback) => {

  request({url:`https://https://rest.coinapi.io/${coin}&ssr=USD`,

  json: true
  }, (error, response, body) => {


    if(error){
      callback("error");

    }
    else if (response.statusCode == 200) {
      let price = body.RAW[coin].USD.PRICE;
      callback(undefined,price);

    }
  })
};

app.get('/', (req, res) => {
  coin.getCoin('BTC', ()=> {
    res.render('index.hbs', {
      coin:'Bitcoin',
      price: coin.getCoin('BTC')

    });
  });
});
请尝试以下代码:

app.get('/', (req, res) => {
    coin.getCoin('BTC', (err, price)=> {
        res.render('index.hbs', {
            coin:'Bitcoin',
            price: price

        });
    });
});

我假设
coin.getCoin
接受两个参数,第二个参数是callback,它本身需要接受args才能正常工作。现在进入
app.get(“/”
您调用了
coin.getCoin
并传递了一个匿名函数,该函数将被视为对它的回调,如果
coin.getCoin
正确地完成了它的工作,那么
price
的值将最终传递给
index.hbs
res.render
将完成其余的工作。

这就是确切的co吗de?
callback
不接受任何参数,但您正在尝试为其提供1和2个参数。
getCoin
如何神奇地成为
coin
的属性?@RaphaMex抱歉,我没有正确显示代码。我导出了文件。问题已解决