Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

205
Visualizações
Node.JS Javascript fetching file from directory returning same file

I'm trying to read data files from a directory called 'myfiles' using a script 'app.js'.

The file to be read depends on the configurations passed to 'app.js' by another process which generates the files 'log.txt' and 'config.json'.

'app.js' then reads the files 'log.txt' and 'config.json' and fetches a data file from 'myfiles' based on the file name received.

The files in 'myfiles' are labeled my-files1.txt, my-files2.txt, my-files3.txt, ... etc.

The problem is, I keep fetching the same file 'my-files1.txt', even though 'log.txt' and 'config.json' provide new names for files to fetch.

Could you please help me spot where the problem is?

Thanks for your help!

app.js:


const fs = require('fs');

const { resolve } = require('path');

function readLog() {
    try { 
        return fs.readFileSync(__dirname + '/' + 'log.txt',  'utf8');
    }   
    catch (err) { 
        return '';
    }       
}

function readConfig() {   
    try { 
        return fs.readFileSync(__dirname + '/' + 'config.json',  'utf8');
    }
    catch (err) { 
        return '';
    }
}

let config = readConfig(); 
let log = readLog();
            
let cfg = JSON.parse(config);
let lcg = JSON.parse(log);

var currentPage = lcg.tPartition; // Current page to fetch
const numberOfPages = 10; 

if((cfg.running!=true) && (Number(currentPage)>Number(numberOfPages))){
    currentPage = 1;
}

function readDataset() {
    try { 
        return fs.readFileSync(resolve(`./app/assets/myfiles/my-files${currentPage}.txt`),  'utf8');
    }
    catch (err) { 
        return err;
    }
}

let dataset = readDataset();

const data = {
    data1: dataset // set dataset as value of data1
}

module.exports={data};

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

you have written

let lcg = JSON.parse(log);

but your log is a txt file how can you parse it, is this the problem let me know if this worked by any chance;

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda