简体   繁体   中英

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

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. You should use the client inside a callback for connect .

Read here about how to connect using this driver Github Docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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