0

I've created this simple program to parse EDI files. It works fine but the node program never exits. It seems to be waiting for something. I've tried closing the fs stream, the mongodb client, and even process.exit() at on end but with no success. I also tried wrapping it in a Promise but that didn't work either.

Is there any way to tell what a nodejs program is waiting on?

const { X12parser, X12grouper, Schema } = require("x12-parser");
const { createReadStream } = require("fs");
const { MongoClient, MongoKerberosError } = require('mongodb');
const url = 'mongodb://localhost:27017';
const client = new MongoClient(url);
const dbName = 'edi';
const dbCollection = 'edi835'
const schemaStart = 'CLP'
const schemaEnd = 'AMT'
const schemaGroup = 'Claim'
const ediFile = './EDI835.edi'

client.connect();
console.log('Connected successfully to server');
const db = client.db(dbName);
const drop = db.collection(dbCollection).drop();
const collection = db.collection(dbCollection);

const schema = {
    start: schemaStart, // What segment starts the group
    end: schemaEnd, // What segment ends the group
    name: schemaGroup, // What is the name of the group
};

const myParser = new X12parser();
const mySchema = new Schema(ediFile, schema);
const myGrouper = new X12grouper(mySchema);

const testFile = createReadStream(ediFile, {
    emitClose: true
});

let count = 0;

testFile
    .pipe(myParser)
    .pipe(myGrouper)
    .on("data", (data) => {
        if (data.name === "Claim") {
            collection.insertOne(data)
            count++
        }
    }).on("end", () => {
        console.log("End of streaming data ...");
        console.log("Record count= ", count)
    }).on("close", () => {
        console.log("Finished")
    });
  • You probably need to close the program in the `on("close"` [How to exit in Node.js](https://stackoverflow.com/q/5266152) – VLAZ Dec 12 '21 at 12:58
  • I tried using process.exit on("close") but it exits before all the pending processing is complete. – Nicholas Steblay Dec 12 '21 at 13:06

1 Answers1

1

The root cause is that you newer disconnect your Mongoose client. Do client.disconnect when all job is done.

Ayzrian
  • 2,279
  • 1
  • 7
  • 14
  • If I put the client.disconnect in the .on("end") or .on("close") the program crashes because the async processes haven't completed. – Nicholas Steblay Dec 12 '21 at 16:53