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

240
Views
Not reading text in a right way node js

I am working on a sales tax problem with node js and I am trying to read text from a file input.txt then calculate the taxes and prices as a final output. I used the function below to read the file input.txt line by line and it's working

      var fs = require('fs'),
  readline = require('readline');
  var rl = readline.createInterface({
    input : fs.createReadStream('input.txt'),
    output: process.stdout,
    terminal: false
  })
  rl.on('line',function(line){
    console.log(line)
    
  })

But the problem is when I tried use the function scanProduct() on that text it didn't work. This is the whole code

    var Cart = require('./src/Cart');


    var cart1 = new Cart();
    var cart2 = new Cart();
    var cart3 = new Cart();

    cart1.scanProduct('1 book at 12.49');
    cart1.scanProduct('1 music CD at 14.99');
    cart1.scanProduct('1 chocolate bar at 0.85');

    cart2.scanProduct('1 imported box of chocolates at 10.00');
    cart2.scanProduct('1 imported bottle of perfume at 47.50');

    // cart3.scanProduct('1 imported bottle of perfume at 27.99');
    // cart3.scanProduct('1 bottle of perfume at 18.99');
    // cart3.scanProduct('1 packet of headache pills at 9.75');
    // cart3.scanProduct('1 box of imported chocolates at 11.25');


    var fs = require('fs'),
    readline = require('readline');
    var rl = readline.createInterface({
      input : fs.createReadStream('input.txt'),
      output: process.stdout,
      terminal: false
    })
    rl.on('line',function(line){
      //console.log(line)
      cart3.scanProduct(line) 
    })

    console.log(cart1.bill());
    console.log("\n");
    console.log(cart2.bill());
    console.log("\n");
    console.log(cart3.bill());

scanProduct() and bill() are fuction used to calculate taxes and give the final output and it's working in cart1 and car2 that way. When I tried to get text from the file it didn't work.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It fails because reading of a file is an asynchronous operation and cart3.bill() function is called before cart3.scanProduct(line) function. You should listen to an 'end' event of a readable stream then call the cart3.bill() function. Like this:

rl.on('end',function(){
  console.log(cart3.bill()); 
})
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!