首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >出现错误"callback is not a function“

出现错误"callback is not a function“
EN

Stack Overflow用户
提问于 2020-12-16 16:48:15
回答 2查看 46关注 0票数 0

我要做的是创建一个名为GetYearByCurrency的函数(它放在“dataFunctions”上),并将特定年份和特定货币的数据保存到我的索引文件中。我在我的"dataFunctions“文件上收到错误"callback is not a function”

我从" data“文件中的API中提取数据,代码如下:

索引文件:

代码语言:javascript
运行
复制
const data = require('./data');
const mssql = require('mssql');
const dataFunctions = require('./dataFunctions');



var AllData;

data.GetData((d)=>{
    AllData=d;
    //var years = Object.keys(AllData.rates)
    //console.log(AllData.rates[years[1]]['EUR']);
    //console.log(AllData);
    console.log(dataFunctions.GetYearByCurrency(d,2016,'USD'));
});

下面的数据文件:

代码语言:javascript
运行
复制
const fetch = require('node-fetch');

const GetData =(callback)=>{
    fetch('https://api.exchangeratesapi.io/history?start_at=2015-01-01&end_at=2020-09-11&base=ILS')
    .then(response => response.json())
    .then(data => { 
    callback(data)
    })
}

module.exports ={
    GetData
}

最后是下面的dataFunctions

代码语言:javascript
运行
复制
 var GetYearByCurrency = (data,year,currency,callback) => {
   var rates = [];
    for (let m = 1; m <= 12; m++) { // Do a loop for each month of the year
        const numM = m < 10 ? `0${m}` : m, //For our method of date filtering, we need to add a leading zero for Jan-Sept
        month = Object.entries(data.rates) // Convert the rates objects to [key, [values]] arrays
          .filter(day => {
            const splitDate = day[0].split("-"); // Bear's method of splitting up the date string so we can compare month and year
            return splitDate[1] == numM && splitDate[0] === "year"; // Filter them to 2019 and the current month
          })
          .map(day => day[1].currency) // Return a new array with only the USD values for that month
          rates.push(month) // Push this array to our result array
          callback(rates);
          
  }
}

module.exports ={
  GetYearByCurrency
}

对如何解决这个问题有什么建议吗?

EN

Stack Overflow用户

发布于 2020-12-16 16:53:56

在这一行上:

代码语言:javascript
运行
复制
console.log(dataFunctions.GetYearByCurrency(d,2016,'USD'));

您需要发送一个回调作为GetYearByCurrency函数的第四个参数:

代码语言:javascript
运行
复制
dataFunctions.GetYearByCurrency(d,2016,'USD', (rates) => {
    console.log(rates);
});
票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65319970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档