1

I used MongoClient.isConnected() method on version 3.6 of mongodb but now the method does not seem to exist anymore. Why?

If the expression MongoClient.isConnected() is not clear I mean this:

const client = await MongoClient.connect(uri, options?);

client.isConnected(); // does not exist on type MongoClient

I am using Typescript, and of course I installed mongodb and its types (even if it is not necessary anymore with mongodb v.4.0).

How do I get to know if my database is connected or not?

Jolan B.
  • 13
  • 5

1 Answers1

0

You can handle the check via callbacks, which you can pass as parameters in the .conenct Method. See the documentation.

Michael
  • 91
  • 3
  • Sure, but in a unit testing context, handling it via callbacks is not a good thing. Is it ? – Jolan B. Aug 31 '21 at 15:54
  • Imo, it depends on what you want to test. If you want to test the connection speed performance, then it wouldn't be a good solution. But if you want to test, if the database is reachable, i can't think of a reason why handling it with a callback would have a negative impact. – Michael Aug 31 '21 at 16:27
  • I'll try but I'm still searching for another option :) – Jolan B. Sep 01 '21 at 17:07
  • How would one do this? – thelearner Mar 24 '22 at 15:07