C
C
CODER5412018-11-18 22:59:43
Node.js
CODER541, 2018-11-18 22:59:43

How to read number from Node.js file?

There is a file in which the number of records in the database. And I need to extract a number from a file: I'm trying like this:

fs.readFile("./files_to_help/QuoteNum.txt",{encoding:'utf8'},(err,data)=>{
             var ret=0+data;
             return ret;

   });

But I get undefinedI need the content to be returned in int format. How to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stockholm Syndrome, 2018-11-18
@CODER541

fs.readFile('./files_to_help/QuoteNum.txt', {encoding:'utf8'}, (err, data) => {
  console.log(parseInt(data.toString()));
});

or synchronous
let result = parseInt(fs.readFileSync('./files_to_help/QuoteNum.txt', {encoding: 'utf8'}));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question