Eliran Hadad
Eliran Hadad

Reputation: 11

getting error "callback is not a function"

what I'm trying to do is to create a function called GetYearByCurrency (it place on "dataFunctions") and get data of a specific year with a specific currency to my index file. I'm getting the error "callback is not a function" on my "dataFunctions" file

I'm pulling the data from an API in "data" file the code below:

index file:

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'));
});

data file below:

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
}

and finally dataFunctions below

 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
}

any suggestions on how to solve it?

Upvotes: 0

Views: 138

Answers (2)

num8er
num8er

Reputation: 19372

You've written GetYearByCurrency as if it is asynchronous, but I don't see any asynchronous operation inside of it to use callback.

Remove callback and return the rates array:

const GetYearByCurrency = (data, year, currency) => { // REMOVED CALLBACK
  const 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
    const ratesFiltered = 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(ratesFiltered) // Push this array to our result array
  }
  return rates;
}

or call method asynchronous way:

dataFunctions.GetYearByCurrency(d, 2016, 'USD', (result) => {
    console.log(result);
});

Upvotes: 0

P.E. Jo&#235;ssel
P.E. Jo&#235;ssel

Reputation: 1443

On this line :

console.log(dataFunctions.GetYearByCurrency(d,2016,'USD'));

your are expected to send a callback as fourth argument of your GetYearByCurrency function:

dataFunctions.GetYearByCurrency(d,2016,'USD', (rates) => {
    console.log(rates);
});

Upvotes: 1

Related Questions