英文:
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论