Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

241
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda