[node.js] mongoError: Topology was destroyed

I met this in kubernetes/minikube + nodejs + mongoose environment. The problem was that DNS service was up with a kind of latency. Checking DNS is ready solved my problem.

_x000D_
_x000D_
const dns = require('dns');_x000D_
_x000D_
var dnsTimer = setInterval(() => {_x000D_
 dns.lookup('mongo-0.mongo', (err, address, family) => {_x000D_
  if (err) {_x000D_
   console.log('DNS LOOKUP ERR', err.code ? err.code : err);_x000D_
  } else {_x000D_
   console.log('DNS LOOKUP: %j family: IPv%s', address, family);_x000D_
   clearTimeout(dnsTimer);_x000D_
   mongoose.connect(mongoURL, db_options);_x000D_
  }_x000D_
 });_x000D_
}, 3000);_x000D_
_x000D_
_x000D_
var db = mongoose.connection;_x000D_
var db_options = {_x000D_
 autoReconnect:true,_x000D_
_x000D_
 poolSize: 20,_x000D_
 socketTimeoutMS: 480000,_x000D_
 keepAlive: 300000,_x000D_
_x000D_
 keepAliveInitialDelay : 300000,_x000D_
 connectTimeoutMS: 30000,_x000D_
 reconnectTries: Number.MAX_VALUE,_x000D_
 reconnectInterval: 1000,_x000D_
 useNewUrlParser: true_x000D_
};
_x000D_
_x000D_
_x000D_

(the numbers in db_options are arbitrary found on stackoverflow and similar like sites)

Examples related to node.js

Hide Signs that Meteor.js was Used Querying date field in MongoDB with Mongoose SyntaxError: Cannot use import statement outside a module Server Discovery And Monitoring engine is deprecated How to fix ReferenceError: primordials is not defined in node UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac internal/modules/cjs/loader.js:582 throw err DeprecationWarning: Buffer() is deprecated due to security and usability issues when I move my script to another server Please run `npm cache clean`

Examples related to mongodb

Server Discovery And Monitoring engine is deprecated Avoid "current URL string parser is deprecated" warning by setting useNewUrlParser to true MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017] Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified Failed to start mongod.service: Unit mongod.service not found db.collection is not a function when using MongoClient v3.0 MongoError: connect ECONNREFUSED 127.0.0.1:27017 MongoDB: How To Delete All Records Of A Collection in MongoDB Shell? How to resolve Nodejs: Error: ENOENT: no such file or directory How to create a DB for MongoDB container on start up?

Examples related to mongoose

Querying date field in MongoDB with Mongoose Server Discovery And Monitoring engine is deprecated Avoid "current URL string parser is deprecated" warning by setting useNewUrlParser to true Mongodb: failed to connect to server on first connect Push items into mongo array via mongoose Mongoose: findOneAndUpdate doesn't return updated document mongoError: Topology was destroyed Difference between MongoDB and Mongoose How can you remove all documents from a collection with Mongoose? E11000 duplicate key error index in mongodb mongoose

Examples related to restify

mongoError: Topology was destroyed Error: getaddrinfo ENOTFOUND in nodejs for get call

Examples related to pm2

Can pm2 run an 'npm start' script mongoError: Topology was destroyed