Node.js - synchronous operations: file read followed by updates

81 Views Asked by At

Probably this is a promise implementation but would like to check with experts before doing so.

Need to do:

  1. Read entire file line-by-line into MongoDB collection A.
  2. Upon completion of step 1, Insert/Update/Delete documents from collection B based on state in collection A. If document not present in A delete from B.

Problem: Even before completion of step 1 above, step 2 starts execution and starts deleting records from B.

Tried so far: Async.series does not work. Below given is my code.

MongoClient.connect(config.mongodb.uri, function (err, db) {
  if (err) {
    logger.error('Unable to connect to the mongoDB server. Error:', err);
    reject(err);
  } else {
    let startTime = new Date();

    async.series([
      function(callback) {
        console.log('First in series');

        db.collection('eligibilityStage').drop({}, function (err, oldObject) {
          debugger;
          var lr = new LineByLineReader(config.eligibiltyFile.fileRemoteLocation +  '/' + latestEligibilityfileName);        
          lr.on('error', function (err) {
            console.log(err);
          });
          var lineCount;
          lr.on('line', function (line) { //** --> Jumps from here to second function in series, line#43**
            if (line.length == config.eligibiltyFile.detailRecordlineWidth) { 

              var document = require('fixy').parse({
                map: mapData, options: {
                  skiplines: null, fullwidth: config.eligibiltyFile.detailRecordlineWidth
                }
              }, line);

              db.collection('eligibilityStage').insertOne(document[0], function (err, records) {
                lineCount++;
                if (err) {
                  console.log(err);
                }
              });
            }
          });        
          lr.on('end', function () {        
            console.log('File is closed, read lines:'+lineCount);
            console.log('File is closed, rowcount:'+db.eigibilityStage.Count());
          });                             
          callback(null, 'loadStage');    
        });
      },
      function(callback) {
        // Deletes
        console.log('Series 2 function, read lines:'+lineCount);
        console.log('Series 2 function, rowcount:'+db.eigibilityStage.Count());    
        callback(null, 'processStage');
      }
    ],
    function(err, results){

    });
  }
})

Am I doing it wrong? Or is this a standard problem to be solved using promise?

0

There are 0 best solutions below