Javascript 我的节点脚本在函数完成后挂起

Javascript 我的节点脚本在函数完成后挂起,javascript,node.js,mongodb,Javascript,Node.js,Mongodb,我正在调用三个函数,在这些函数完成后,我希望我的脚本自己关闭,但它只是挂起 我尝试过使函数异步/基于承诺,在每个“mongodb”类型的函数之后关闭数据库,并在函数中使用process.exit()作为对上次调用函数的回调 连接到(本地-非Atlas)数据库: MongoClient.connect(local, {useNewUrlParser: true, useUnifiedTopology: true}, function(err, db) { if (err) { cons

我正在调用三个函数,在这些函数完成后,我希望我的脚本自己关闭,但它只是挂起

我尝试过使函数异步/基于承诺,在每个“mongodb”类型的函数之后关闭数据库,并在函数中使用process.exit()作为对上次调用函数的回调

连接到(本地-非Atlas)数据库:

MongoClient.connect(local, {useNewUrlParser: true, useUnifiedTopology: true}, function(err, db) {
  if (err) {
    console.log(err)
  }
  else {
    console.log('Connected to MongoDB...')
    //Read in data from jsonfiles and store each file's contents into the database : This is where the functions are being called... within a successful connect to the MongoDB
    insertJSON(db, jsonfiles, 'requests', jsonfilesSource)
    insertJSON(db, issuedfiles, 'issuedLicenses', isssuedfilesSource)
    insertLicenses(db)
  }
  db.close()
})
职能1:

function insertJSON(db, dirBuf,collection, sourceFolder) {
  var database = db.db('license-server')
  var collection = database.collection(collection)
  fs.readdir(dirBuf, function(err, files) {
    if (err) {
      console.log(err.message)
    }
    else {
      files.forEach(function(filename) {
        var text = fs.readFileSync(sourceFolder + filename);
        var filecontents = JSON.parse(text)
        //collection.insertOne(filecontents)
        collection.findOne({"DisplayTitle" : filecontents.DisplayTitle, "NodeInformation" : filecontents.NodeInformation, "Date": filecontents.Date})
          .then(function(result) {
            if(result) {
              console.log(`An Item could already be in the database: A file is unique if its display title, nodeinformation, and date are different.
              the items display title is ${result.DisplayTitle}`)
              return
            }
            else {
              collection.insertOne(filecontents)
              console.log(`Added ${filecontents.DisplayTitle} to database`)
            }
          })
          .catch(function(error) {
            console.log(error)
          })
      })
    }
  })
}
职能2:

function insertLicenses(db) {
  // Set up GridFS to import .lic and .licx files into the database
  var database = db.db('license-server')
  var collection = database.collection('fs.files')
  var bucket = new mongodb.GridFSBucket(database);
  var dirBuf = Buffer.from('../license-server/private/licenses')
  fs.readdir(dirBuf, function(err, files) {
    if (err) {
      console.log(err.message)
    }
    else {
      files.forEach(function(filename) {
        collection.findOne({"filename": filename}).
        then(function(result) {
          if(result) {
            console.log(`The file ${filename} is already in the database`)
            return
          }
          else {
            fs.createReadStream('./private/licenses/' + filename).
            pipe(bucket.openUploadStream(filename)).
            on('error', function(error) {
              assert.ifError(error)
            }).
            on('finish', function() {
              console.log(`Uploaded ${filename}`)
            })
          }
        })
      })
    }
  })
// I tried calling db.close() here since this is the last function to be called. No luck.
}
我猜这与mongodb函数有自己的关闭方式有关,但我似乎找不到我在以前解决这个问题的尝试中所寻找的内容


预期的结果应该是脚本自身关闭,实际的结果是处理脚本。

所有这些数据库调用都是异步的——运行此代码的结果是立即调用
db.close
,然后在
insertJSON
insertLicenses
中执行工作。如果要重写此命令以使用
async/await
(您还需要更新其他函数),
db.close
调用将关闭数据库,这将允许脚本退出:

  await insertJSON(db, jsonfiles, 'requests', jsonfilesSource)
  await insertJSON(db, issuedfiles, 'issuedLicenses', isssuedfilesSource)
  await insertLicenses(db)
  db.close()