MongoBulkWriteError: 尝试从已关闭的连接池中检出连接

huangapple go评论54阅读模式
英文:

MongoBulkWriteError: Attempted to check out a connection from closed connection pool

问题

我想要从我的Node.js文件插入一些数据到MongoDB数据库中,但我收到以下错误消息。我该如何解决这个问题?我在MongoDB和Node.js方面都是初学者。我在Stack Overflow上搜索了一些文档和帖子,但都没有适用于我。

以下是我执行的命令:

$ FruitsProject % node app.js

这是我的终端上的消息:

成功连接到服务器!
完成。
/Users/xxxxxx/Desktop/FruitsProject/node_modules/mongodb/lib/bulk/common.js:330
            return callback(new MongoBulkWriteError(err, new BulkWriteResult(bulkOperation.s.bulkResult)));
                            ^

MongoBulkWriteError: 尝试从关闭的连接池中检出连接
    at resultHandler (/Users/xxxxxx/Desktop/FruitsProject/node_modules/mongodb/lib/bulk/common.js:330:29)
    at /Users/xxxxxx/Desktop/FruitsProject/node_modules/mongodb/lib/utils.js:282:66
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  address: '127.0.0.1:27017',
  writeErrors: [],
  result: BulkWriteResult {
    insertedCount: 0,
    matchedCount: 0,
    modifiedCount: 0,
    deletedCount: 0,
    upsertedCount: 0,
    upsertedIds: {},
    insertedIds: {
      '0': ObjectId {
        [Symbol(id)]: Buffer(12) [Uint8Array] [
          100,  91, 252, 154, 95,
           37,  33,  75, 234, 61,
           96, 249
        ]
      },
      '1': ObjectId {
        [Symbol(id)]: Buffer(12) [Uint8Array] [
          100,  91, 252, 154, 95,
           37,  33,  75, 234, 61,
           96, 250
        ]
      }
    }
  },
  [Symbol(errorLabels)]: Set(0) {}
}

Node.js v18.15.0
英文:

I want to insert some data from my nodejs file on the mongodb database.
But I get the error message as below. How can I make this work?
I am pretty much of a beginner in mongodb and nodejs.
I searched some documents and threads on stackoverflow but none of them worked for me.


const {
  MongoClient
} = require('mongodb');
// or as an es module:
// import { MongoClient } from 'mongodb'

// Connection URL
const url = 'mongodb://127.0.0.1:27017';
const client = new MongoClient(url);

// Database Name
const dbName = 'fruitsDB';

async function main() {
    // Use connect method to connect to the server
    await client.connect();
    console.log('Connected successfully to server!!!!');
    const db = client.db(dbName);
    const collection = db.collection('fruits');

    // the following code examples can be pasted here...
    collection.insertMany([
        {
          name: "Apple",
          score: 8,
          review: "Great fruit"
        },
        {
          name: "Orange",
          score: 6,
          review: "Kinda sour"
        }
      ])

      return 'done.';
}

main()
  .then(console.log)
  .catch(console.error)
  .finally(() => client.close());

And this is the commmand I executed.

$ FruitsProject % node app.js

This is the message on my terminal.

Connected successfully to server!!!!
done.
/Users/xxxxxx/Desktop/FruitsProject/node_modules/mongodb/lib/bulk/common.js:330
            return callback(new MongoBulkWriteError(err, new BulkWriteResult(bulkOperation.s.bulkResult)));
                            ^

MongoBulkWriteError: Attempted to check out a connection from closed connection pool
    at resultHandler (/Users/xxxxxx/Desktop/FruitsProject/node_modules/mongodb/lib/bulk/common.js:330:29)
    at /Users/xxxxxx/Desktop/FruitsProject/node_modules/mongodb/lib/utils.js:282:66
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  address: '127.0.0.1:27017',
  writeErrors: [],
  result: BulkWriteResult {
    insertedCount: 0,
    matchedCount: 0,
    modifiedCount: 0,
    deletedCount: 0,
    upsertedCount: 0,
    upsertedIds: {},
    insertedIds: {
      '0': ObjectId {
        [Symbol(id)]: Buffer(12) [Uint8Array] [
          100,  91, 252, 154, 95,
           37,  33,  75, 234, 61,
           96, 249
        ]
      },
      '1': ObjectId {
        [Symbol(id)]: Buffer(12) [Uint8Array] [
          100,  91, 252, 154, 95,
           37,  33,  75, 234, 61,
           96, 250
        ]
      }
    }
  },
  [Symbol(errorLabels)]: Set(0) {}
}

Node.js v18.15.0

答案1

得分: 1

我对MongoDB不太熟悉,但也许你需要在返回'done'之前等待插入,尝试这样做:

await collection.insertMany([.....
英文:

I'm not familiar with MongoDB, but maybe you have to await the insert , before returning 'done.'
Try this:

await collection.insertMany([ .....


</details>



huangapple
  • 本文由 发表于 2023年5月11日 04:47:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76222445.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定