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