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

242
Visualizações
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 Respostas
Responde à pergunta

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 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