checkConnection
talawa-api • Docs
talawa-api / setup/MongoDB / checkConnection
Function: checkConnection()
> checkConnection(url
): Promise
<boolean
>
The checkConnection
function attempts to establish a connection to a MongoDB instance using a provided URL.
Parameters
• url: string
The MongoDB connection URL.
Returns
Promise
<boolean
>
A promise that resolves to a boolean indicating whether the connection was successful (true) or not (false).
It performs the following steps:
- Tries to establish a connection to the MongoDB instance using the provided URL with a server selection timeout of 1000 milliseconds.
- If the connection is successful, it closes the connection and returns true.
- If the connection fails, it logs an error message and returns false.
- If the error is an instance of the Error class, it logs the error message.
- If the error is not an instance of the Error class, it logs a generic error message and the error itself.
This function is used during the initial setup process to test the MongoDB connection.