如何使用JS / node正确使用async / await

时间:2018-02-07 08:21:44

标签: javascript node.js mongodb async-await

我正在尝试创建一个脚本,该脚本将使用从几个API调用返回的数据填充我的Mongo数据库。理想情况下,这将每隔一段时间手动运行一次,或者在调度程序上运行,以使用新数据更新数据库,因此我尝试在“MongoClient.conncect”函数内部执行所有操作。我想一次导入这个数据一个API调用,并试图避免链接承诺和.then的丑陋。我已经做了一些关于使用async / await的研究,在我看来,使用'await'关键字实际上是展开返回的promise。如果是这种情况,我无法弄清楚为什么它不能正常等待。以下是我到目前为止的情况:

const MongoClient = require('mongodb').MongoClient;
const fs = require('fs');
const assert = require('assert');
const ProPublicaAPI = require('./ProPublicaAPI');
var constants = require('../common/constants');
var https = require('https');

const url = 'mongodb://localhost:27017';


dbName = 'government';

MongoClient.connect(url, function(err, client) {
  assert.equal(null, err);
  console.log('successfully connected to mongoDB server: ' + url);

  const db = client.db(dbName);
  try{
    var senateInsert = waitInsert(db, 'senate');
  }
  catch(err) {
    console.log(err);
  }
  console.log('closing database connection');
  client.close();
})

async function waitInsert(db, collection) {
  //I made this function just to call inside the MongoClient.connect() above
  return await insertSenateDocuments(db, collection);
}

function insertSenateDocuments(db, chamber) {
  return new Promise(function(resolve) {
    var collection = db.collection(chamber);
    //method that calls API, to be converted into async/await later
    senateMembers = ProPublicaAPI.getAllMembersByChamber(chamber);
    senateMembers.then(function(promisedMembers) {
      console.log("Recieved data from api");
      collection.insertMany(promisedMembers, function(err, result) {
        if(err) return console.log(err);
        console.log(`Imported ${chamber} data`);
        resolve(result);
      });
    })
  })
}

这是堆栈跟踪:

successfully connected to mongoDB server: mongodb://localhost:27017
about to close
Recieved collection Data
{ MongoError: server instance pool was destroyed
    at basicWriteValidations (D:\Dev\node\node_modules\mongodb-core\lib\topologies\server.js:636:41)
    at Server.insert (D:\Dev\node\node_modules\mongodb-core\lib\topologies\server.js:737:16)
    at Server.insert (D:\Dev\node\node_modules\mongodb\lib\topologies\topology_base.js:315:25)
    at executeCommands (D:\Dev\node\node_modules\mongodb\lib\bulk\ordered.js:525:23)
    at executeOperation (D:\Dev\node\node_modules\mongodb\lib\utils.js:408:22)
    at OrderedBulkOperation.execute (D:\Dev\node\node_modules\mongodb\lib\bulk\ordered.js:602:10)

    at bulkWrite (D:\Dev\node\node_modules\mongodb\lib\collection.js:627:8)
    at executeOperation (D:\Dev\node\node_modules\mongodb\lib\utils.js:408:22)
    at Collection.insertMany (D:\Dev\node\node_modules\mongodb\lib\collection.js:512:10)
    at D:\Dev\node\dataimporter\mongo.js:39:18
  name: 'MongoError',
  message: 'server instance pool was destroyed' }
PS D:\Dev\node\dataimporter> node mongo
successfully connected to mongoDB server: mongodb://localhost:27017
closing database connection
Recieved data from api
{ MongoError: server instance pool was destroyed
    at basicWriteValidations (D:\Dev\node\node_modules\mongodb-core\lib\topologies\server.js:636:41)
    at Server.insert (D:\Dev\node\node_modules\mongodb-core\lib\topologies\server.js:737:16)
    at Server.insert (D:\Dev\node\node_modules\mongodb\lib\topologies\topology_base.js:315:25)
    at executeCommands (D:\Dev\node\node_modules\mongodb\lib\bulk\ordered.js:525:23)
    at executeOperation (D:\Dev\node\node_modules\mongodb\lib\utils.js:408:22)
    at OrderedBulkOperation.execute (D:\Dev\node\node_modules\mongodb\lib\bulk\ordered.js:602:10)
    at bulkWrite (D:\Dev\node\node_modules\mongodb\lib\collection.js:627:8)
    at executeOperation (D:\Dev\node\node_modules\mongodb\lib\utils.js:408:22)
    at Collection.insertMany (D:\Dev\node\node_modules\mongodb\lib\collection.js:512:10)
    at D:\Dev\node\dataimporter\mongo.js:39:18
  name: 'MongoError',
  message: 'server instance pool was destroyed' }

如您所见,在插入文档之前正在调用client.close()。

2 个答案:

答案 0 :(得分:3)

  

artist

您必须musicdb
await

答案 1 :(得分:0)

更容易:

async bulkWriteItems(array, collection) {
    return await collection.bulkWrite(array, {ordered: true, w: 1})
}

致电:

async saveData() {
    let bulk = [];
    for (let i = 0; i < 100; i++) {
        let name = `item${i}`
        bulk.push({insertOne: {name}});
    }
    const collection = await db.collection(COLLECTION_OBJECTS)
    await this.bulkWriteItems(bulk, collection);
}