0

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?

2 Answers 2

1

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);
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! that was the problem
@EliranHadad if You go by callback way, see Your code is wrong, put callback to outside of for loop, otherwise it will call callback on every iteration.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.