If the connect
function failed (i.e. err != null
) that means the connection state is not open, therefore, you can't close or disconnect it.
To prove it to yourself, you can check the mongoose.connection.readyState
(here are the available values).
May I suggest using mongoose.connection.on('error', cb);
to better handle connection errors.