Warning: Accessing non-existent property 'arrayLength' of module exports inside circular dependency

Viewed 44

I'm trying to import my controller arrayLength() function to my router file but I keep getting this error : TypeError: Cannot read property 'charsLength' of undefined And this warning: Accessing non-existent property 'arrayLength' of module exports inside circular dependency

Tips: When I console.log(challengeController) inside router file it returns an empty object {}

When i import challengeController.arrayLength() function to my app.js it works fine!

This is my controller file:

const challengeRoutes = require('./challengeRoutes');

module.exports = {
    arrayLength: async() => {

        const lengths = await challengeRoutes.arrayLengths();

        console.log(lengths)

        //Devuelve array con todos los numeros entre 1 y num
        let arrayLengthCounter = (num) => {
            let numbers = [];
            for(let i = 1; i <= num; i++){
              numbers.push(i);
            }
            return numbers;
        }  
        
        const charsLength = arrayLengthCounter(lengths[0]);
        const locationsLength = arrayLengthCounter(lengths[1]);
        const episodesLength = arrayLengthCounter(lengths[2]);
        
        return {charsLength, locationsLength, episodesLength};
    },
}

And this is my router file where I'm trying to import my challengeController.arrayLength() function:

const axios = require('axios');
const challengeController = require('./challengeController');
const url = 'https://rickandmortyapi.com/api';


module.exports = {
    arrayLengths: async() => {
        try{
            const result = await Promise.all([
                axios.get(url + '/character').then((res) => res.data.info.count),
                axios.get(url + '/location').then((res) => res.data.info.count),
                axios.get(url + '/episode').then((res) => res.data.info.count),
            ]);
          return result;
        }
        catch(e){
            console.log(e);
        }
    },
    getFullArray: async() => {
        try{
            const lengths = challengeController.arrayLength();

            const result = await Promise.all([
            axios.get(url + '/character/' + lengths.charsLength).then((res) => res.data),
            axios.get(url + '/location/' + lengths.locationsLength).then((res) => res.data),
            axios.get(url + '/episode/' + lengths.episodesLength).then((res) => res.data),
            ]);
            return result;
        }
        catch(e){
            console.log(e);
        }
    },
}

Any idea what I'm doing wrong?

0 Answers
Related