【发布时间】:2019-11-27 00:38:40
【问题描述】:
我尝试将节点应用程序中的数据存储移动到 mongo db。但我对 mongo db 有一个简单的问题。
我有一个按钮,点击网站,会调用/datastore
app.post('/datastore', (req, res) => {
client.connect(err => {
var dbo = client.db("test");
dbo.listCollections().toArray(function(err, items){
if (err) throw err;
console.log(items);
if (items.length == 0)
console.log("No collections in database")
});
client.close();
});
});
我第一次点击按钮时效果很好。但是,如果我第二次单击该按钮,我会收到错误消息:
不支持选项 [servers] 选项 [caseTranslate] 是 不支持的选项 [dbName] 不支持的选项 [凭据] 不受支持 /Users/ingofoerster/Downloads/development/testrunner/node_modules/mongodb/lib/utils.js:132 抛出错误; ^
MongoError: 拓扑被破坏 在 initializeCursor (/Users/ingofoerster/Downloads/development/testrunner/node_modules/mongodb-core/lib/cursor.js:596:25) 在 nextFunction (/Users/ingofoerster/Downloads/development/testrunner/node_modules/mongodb-core/lib/cursor.js:456:12) 在 CommandCursor.Cursor.next (/Users/ingofoerster/Downloads/development/testrunner/node_modules/mongodb-core/lib/cursor.js:766:3) 在 CommandCursor.Cursor._next (/Users/ingofoerster/Downloads/development/testrunner/node_modules/mongodb/lib/cursor.js:216:36) 在 fetchDocs (/Users/ingofoerster/Downloads/development/testrunner/node_modules/mongodb/lib/operations/cursor_ops.js:217:12) 在 toArray (/Users/ingofoerster/Downloads/development/testrunner/node_modules/mongodb/lib/operations/cursor_ops.js:247:3) 在执行操作(/Users/ingofoerster/Downloads/development/testrunner/node_modules/mongodb/lib/utils.js:416:24) 在 CommandCursor.Cursor.toArray (/Users/ingofoerster/Downloads/development/testrunner/node_modules/mongodb/lib/cursor.js:829:10) 在 client.connect.err (/Users/ingofoerster/Downloads/development/testrunner/start.js:256:35) 结果(/Users/ingofoerster/Downloads/development/testrunner/node_modules/mongodb/lib/utils.js:410:17) 在 executeCallback (/Users/ingofoerster/Downloads/development/testrunner/node_modules/mongodb/lib/utils.js:402:9) 在错误(/Users/ingofoerster/Downloads/development/testrunner/node_modules/mongodb/lib/operations/mongo_client_ops.js:286:5) 在 connectCallback (/Users/ingofoerster/Downloads/development/testrunner/node_modules/mongodb/lib/operations/mongo_client_ops.js:265:5) 在 topology.connect (/Users/ingofoerster/Downloads/development/testrunner/node_modules/mongodb/lib/operations/mongo_client_ops.js:417:5) 在 ReplSet.connectHandler (/Users/ingofoerster/Downloads/development/testrunner/node_modules/mongodb/lib/topologies/replset.js:343:9) 在 Object.onceWrapper (events.js:281:20) 在 ReplSet.emit (events.js:193:13) 在 /Users/ingofoerster/Downloads/development/testrunner/node_modules/mongodb-core/lib/topologies/replset.js:786:18 在 processTicksAndRejections (internal/process/task_queues.js:79:9)
我无法解释为什么会发生这种情况,因为我的代码中有 client.close()。知道为什么我不能多次调用该函数吗?
【问题讨论】:
-
试试
return client.close()