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

243
Views
No leer el texto de forma correcta node js

Estoy trabajando en un problema de impuestos sobre las ventas con el nodo js y estoy tratando de leer el texto de un archivo input.txt y luego calcular los impuestos y los precios como resultado final. Usé la función a continuación para leer el archivo input.txt línea por línea y está funcionando

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

Pero el problema es que cuando intenté usar la función scanProduct() en ese texto, no funcionó. este es todo el codigo

 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() y bill() son funciones que se utilizan para calcular los impuestos y dar el resultado final y funcionan en cart1 y car2 de esa manera. Cuando traté de obtener texto del archivo, no funcionó.

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

0

Falla porque la lectura de un archivo es una operación asíncrona y la función cart3.bill() se llama antes que la función cart3.scanProduct(line) . Debe escuchar un evento de 'finalización' de un flujo legible y luego llamar a la función cart3.bill() . Como esto:

 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!