Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

204
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!