MongoError: topology was destroyed#!/usr/bin/env node
// Initialization parameters
var http = require('http');
var MongoClient = require('mongodb').MongoClient;
// polluterTenantId needs to be obtained from DB.
function GetTenantIDFromName(exp, db){
let tID = db.collection('Tenant').find({ "Name": { $regex: rxPtt }}, {"_id": 1}).toArray();
return tID;
}
MongoClient.connect(url, function(err, db) {
if(err) throw err;
let tID = GetTenantIDFromName(tenant, db);
tID.then(function(polluterTenantId){
if (del !== true && polluterTenantId.length > 1){
throw "There was more than 1 tenant";
db.close(function(){ console.log("Close connection"); });
} else if (del !== true && polluterTenantId.length < 1) {
throw "No tenats matched the pattern" + tenant + ".";
db.close(function(){ console.log("Close connection"); });
} else {
// some more inserts / finds / collection creations
}
});
db.close(function(){ console.log("Close connection"); });
});Hi
In your script as posted, you have some undefined variables that I assume was defined in some other parts of the code (e.g. url, rxPtt, del).
However, ignoring all the undefined variables, there are a couple of things that I can see in the code:
throw will stop function execution (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/throw), so the db.close(...) after throw will never be executed. This can be confirmed by running eslint on the code snippet (it will output warning: Unreachable code on those lines).db.close() outside of the tID.then(...) but immediately follows it, which means that the code is closing the database before the query have a chance to come back.I believe the message topology was destroyed was caused by #2 above. To verify this, you could put some identifying numbers to the Close connection strings so that you can identify precisely which db.close() was executed and in what order. For example, you can change the three Close connection strings above to Close connection 1, Close connection 2, and Close connection 3 from top to bottom. I believe you will first see Close connection 3 string printed to the console before the topology was destroyed message.
Best regards,
Kevin