Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
node script not getting completed

I have a simple script to dump data into db, which I am using in conjunction of npm script. I am trying to dump data into db before my node application server starts. However the dump script doesn't complete ever and hence the server start script doesn't start. Here is my dump script -

const mongodb = require('mongodb')
  , fs = require('fs')
  , MongoClient = mongodb.MongoClient
  , client = new MongoClient(`mongodb://localhost:27017/admin`, { useUnifiedTopology: true })
  , docs = fs.readFileSync(`${process.cwd()}/app/scripts/records.json`)

client.connect().then(cl => {
  const dbObj = cl.db('dumpdb')
  dbObj.collection(`loc_biz_hrs`).insertMany(JSON.parse(docs)).then(result => {
    console.log(`${result.insertedCount} records inserted`)
  })
})

and here is how I am trying to execute it using npm script -

"scripts": {
    "start": "node app/scripts/records.js && nodemon --exec babel-node index.js",
    "test": "test"
}

It just displays in terminal 35755 records inserted and gets stuck in there and doesn't execute the index.js part of the script.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

try using concurrently npm package npm i concurrently


"scripts": {
    "start": "react-scripts start",
    "dev": ""concurrently \"node app/scripts/records.js\" \"nodemon --exec babel-node index.js\""
}
over 4 years ago · Santiago Trujillo Report

0

You are not closing the connection using cl.close(), so try this:

const mongodb = require('mongodb')
  , fs = require('fs')
  , MongoClient = mongodb.MongoClient
  , client = new MongoClient(`mongodb://localhost:27017/admin`, { useUnifiedTopology: true })
  , docs = fs.readFileSync(`${process.cwd()}/app/scripts/records.json`)

client
  .connect()
  .then(cl => {
    const dbObj = cl.db('dumpdb');

    dbObj
      .collection(`loc_biz_hrs`)
      .insertMany(JSON.parse(docs))
      .then(result => {
        console.log(`${result.insertedCount} records inserted`);
        cl.close();
      }).catch(error => {
        console.log(error);
        cl.close();
      })
  })
  .catch(error => {
    console.log(error);
  });
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!