【问题标题】:Nodejs connect mongoNodejs连接mongo
【发布时间】: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()

标签: node.js mongodb


【解决方案1】:

当您第一次单击按钮时,您可以按预期获得结果,但在获得结果后,您还通过调用关闭连接 client.close(); ,这不会让 MongoDB 第二次重新连接。

理想情况下,您应该重用现有连接,而不是为每个 API 调用调用 connect 方法。

Connection pooling example is taken from here

var express = require('express');
var mongodb = require('mongodb');
var app = express();

var MongoClient = require('mongodb').MongoClient;
var db;

// Initialize connection once
MongoClient.connect("mongodb://localhost:27017/integration_test", function(err, database) {
  if(err) throw err;

  db = database;

  // Start the application after the database connection is ready
  app.listen(3000);
  console.log("Listening on port 3000");
});

// Reuse database object in request handlers
app.get("/", function(req, res) {
  db.collection("replicaset_mongo_client_collection").find({}, function(err, docs) {
    docs.each(function(err, doc) {
      if(doc) {
        console.log(doc);
      }
      else {
        res.end();
      }
    });
  });
});

【讨论】:

    猜你喜欢
    • 2017-11-25
    • 2022-12-21
    • 1970-01-01
    • 2016-02-16
    • 2012-01-01
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 2014-11-05
    相关资源
    最近更新 更多