MongoError: Topology was destroyed
I can find data from mongo but when update i received error: MongoError: Topology was destroyed
My config:
`
import config from '../../index'
import mongoose from 'mongoose'
import bluebird from 'bluebird'
import uriFormat from 'mongodb-uri'
mongoose.Promise = bluebird
let opts = { useNewUrlParser: true, autoIndex: false, reconnectTries: Number.MAX_VALUE, reconnectInterval: 500, connectTimeoutMS: 10000, socketTimeoutMS: 60000, poolSize: 10, bufferMaxEntries: 0, promiseLibrary: bluebird }, connections = {}
function init(dbName) {
try {
let connection = mongoose.createConnection(Database.encodeMongoURI(config[dbName]['url']), opts)
connection.once('open', function () {
console.log(Connection to ${dbName} established.)
})
connection.on('error', function (err) {
if (err.message && err.message.match(/failed to connect to server .* on first connect/)) {
setTimeout(function () {
console.log("Retrying first connect...")
connection.openUri(Database.encodeMongoURI(config[dbName]['url']), opts).catch(() => {})
}, 20 * 1000)
} else {
console.error(new Date(), String(err))
}
});
connections[dbName] = connection
return connections[dbName]
} catch (e) {
console.log( [${new Date()}] ${config.service_name} "#in config > mongo > General#"] #"${e.message || e}"#)
}
}
class Database {
static getInstance(dbName) {
try {
if (connections[dbName]) {
return connections[dbName]
}
return init(dbName)
} catch (e) {
console.log([${new Date()}] ${config.service_name} ["#in config > mongo > General#"] #"${e.message || e}"#)
}
}
static encodeMongoURI (urlString) {
if (urlString) {
let parsed = uriFormat.parse(urlString)
urlString = uriFormat.format(parsed)
}
return urlString
}
}
export default Database `
export default class HotelCalendarRepository { constructor() { this._connection = Database.getInstance(config.keys.mongo_python) this._logConnection = Database.getInstance(config.keys.mongo_nodejs) } }
I dont know why? Pls help me