简体   繁体   English

在使用MongoDB调用close后,Node.js再次连接

[英]Node.js connecting again after calling close with MongoDB

Using the official and latest version of the Node.js MongoDB driver 3.2.4 and doing 使用官方和最新版本的Node.js MongoDB驱动程序3.2.4并做

const MongoClient = require('mongodb').MongoClient;
const client = new MongoClient(url, {
    useNewUrlParser: true,
    connectTimeoutMS: 10000,
    poolSize: 10,
    j: true,
    reconnectInterval: 2000,
    reconnectTries: 150
});
client.connect();

setTimeout(() => {
   client.close();

   setTimeout(() => {
       client.connect();
   }, 1000);
}, 1000);

I would expect this to connect, wait a second, disconnect, wait a second, and then connect again. 我希望这可以连接,等待一秒,断开连接,等待一秒,然后重新连接。

However I am getting the error: 但是我收到错误:

the options [servers] is not supported
the options [caseTranslate] is not supported
server instance pool was destroyed

Is it not possible to connect again after calling close? 呼叫关闭后是否无法再次连接?

Your script doesn't wait for mongo to actually connect. 您的脚本不会等待mongo实际连接。 You should use the client inside a callback for connect . 您应该在回调中使用client进行connect

Read here about how to connect using this driver Github Docs 请阅读此处有关如何使用此驱动程序Github Docs进行连接的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM