简体   繁体   中英

Javascript read from file

This nodejs file is suppose to read a file line by line. Each line represents and object that I create and add to an array. After finished reading the file it should return that array. Not an expert on javascript but this seems to return an empty array each time. I thought it had something to do with global but creating a temp array and pushing to it in parseline() didn't work either. What am I doing wrong?


var exports = module.exports = {};
const lineReader = require('line-reader');
const Obj = require("./Data")
const Data = Obj.Data;
var records = [];

exports.readAllLines = async function() {
    await lineReader.eachLine('./datafile.dat', function(line) {
        parseLine(line);
    });

    return records;
}

function parseLine(inputLine) {
    var splitArray = inputLine.split("\t");
    var date = new Date(Date.parse(splitArray[0]));

    var o= splitArray[1];
    var h= splitArray[2];
    var l= splitArray[3];
    var c= splitArray[4];
    var v= splitArray[5];

    var dataObject = new Data (date, o, h, l, c, v);
    records.push(dataObject);
}

Calling Code

var readFiles = require("./ReadFile.js");

readFiles.readAllLines().then(function(result) {
    console.log(result);
});

A simple solution using native apis

var fs = require('fs');
let fileArray = fs.readFileSync(filepath).split('\n');

As per line-reader docs

eachLine and open are compatible with promisify from bluebird

So in order to wait for each line to finish then return data you can install bluebird as per the example and change your code to be like the below

var exports = module.exports = {};
const lineReader = require('line-reader');
const Obj = require("./Data")
const Data = Obj.Data;
Promise = require('bluebird');
var eachLine = Promise.promisify(lineReader.eachLine);
var records = [];

exports.readAllLines = async function() {


    await eachLine('./datafile.dat', function (line) {
        parseLine(line);

    });

    return records;

}


function parseLine(inputLine) {

    var splitArray = inputLine.split("\t");
    var date = new Date(Date.parse(splitArray[0]));

    var o= splitArray[1];
    var h= splitArray[2];
    var l= splitArray[3];
    var c= splitArray[4];
    var v= splitArray[5];

    var dataObject = new Data (date, o, h, l, c, v);
    records.push(dataObject);

}

Thanks to jfriends00 and others this is what I came up with. It was indeed a race condition where the array was being returned before the file was read.

var exports = module.exports = {};
const fs = require('fs');
const readline = require('readline');
const Obj = require("./Data")
const Data = Obj.Data;

exports.readAllLines = async function processLineByLine() {
    var records = [];
    const fileStream = fs.createReadStream('./datafile.dat');
    const rl = readline.createInterface({
        input: fileStream,
        crlfDelay: Infinity
    });
    for await (const line of rl) {
        records.push(parseLine(line));
    }

    return records;
}

function parseLine(inputLine, records) {
    var splitArray = inputLine.split("\t");

    var date = new Date(Date.parse(splitArray[0]));
    var o= splitArray[1];
    var h= splitArray[2];
    var l= splitArray[3];
    var c= splitArray[4];
    var v= splitArray[5];

    return new Data(date, o, h, l, c, v);
}

Calling Code

var readFiles = require("./ReadFile.js");

readFiles.readAllLines().then(result => {
    console.log(result);
}).catch(exception => {
    console.log(exception);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM