Read row by row from file . txt

Asked

Viewed 927 times

2

I am trying to read a file . txt from 3.5GB line to line and save to Mongo db. The columns use the "|" tab and I am using Node js with Express. I did the test with a file smaller than 43 lines and it worked all right but with the bigger file I am not getting and I don’t get error message. I was able to save the same file in Mysql but with Mongo not getting it. The request takes a while then the server drops and nothing happens. The code is as follows:

const express = require('express');
const router = express.Router();
const Salario = require('../models/Salarios');
const lineReader = require('line-reader');

let i = 0;

    router.get('/import', function(req, res) {
        lineReader.eachLine('./teste.txt', function (line, last) {

            let str = line;
            let arr = str.split("|");

            let salario = new Salario();
            salario.cd_ugestora = arr[0];
            salario.de_ugestora = arr[1];
            salario.de_cargo = arr[2];
            salario.de_tipocargo = arr[3];
            salario.cd_cpf = arr[4];
            salario.dt_mesanorefencia = arr[5];
            salario.no_servidor = arr[6];
            salario.vl_vantagens = arr[7];
            salario.de_uorcamentaria = arr[8];

            salario.save(function(err) {
                if (err) res.send(err); 
                console.log(i + " - adicionado!");
                i++;
            });

            if (last) {
                res.json({ message: 'Importado com sucesso!' });
            }

        });
    });

Running top on terminal:inserir a descrição da imagem aqui

Someone’s been through it?

1 answer

1


To be sure of the file path you can use var caminho = __dirname + '/teste.txt';. So make sure the lineReader doesn’t change the path.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.