I am getting this error: 'TypeError: callback is not a function', but the function is still executing

I am getting this error: 'TypeError: callback is not a function', but the function is still executing

我收到“类型错误:尝试执行函数时回调不是函数。

我的代码:

const api = require('axios');

getData(printData);


async function getData(callback) {
  try {
    const results = await api.get('https://sampleurl.com//wp-json/api/data');
    const dataArray = results.data.items;
    callback(dataArray);
  } catch (err) {console.error(err)}
}

function printData(data) {
  console.log('Got data:', data.length);
}

module.exports = getData();

我在控制台中收到此响应:

TypeError: callback is not a function
    at getLabiExams (/backend/src/scrapers/xxx/getData.js:10:5)
    at processTicksAndRejections (internal/process/task_queues.js:94:5)
Got data: 698

请注意该函数仍在正常工作,尽管它返回 'callback is not a function' 错误。我在这里做错了什么?

module.exports = getData();

你不小心在这里调用了你的函数。由于此时没有参数传递给它,callback 未定义,因此不是函数。