I am trying to connect to my mongodb from node/express and I am receiving a connection timed out error when trying to connect. This is the code I am working with atm to find the solution.
await client.connect().then((res:any) => console.log(res)
And this is the error code given.
MongoServerSelectionError: connect ETIMEDOUT 52.64.110.205:27017
So far I have tried adding additional timeout params including
keepAlive=true&socketTimeoutMS=360000&connectTimeoutMS=360000
I have also tried connecting to another cluster with a different username/password and received the same error. I don't think it's an error with env variables as all the other .env variables are working. And I think it might be worth mentioning that this function was working for the first day or two after I put it in.
Below is the entire function. I have commented some parts out for debugging purposes.It returns the same error either way, so I assume it can only be something to do with the connection.
export const handleCreateRequestDB = async (input: any) => {
console.log(`creating new user in DB @ ${input}`)
const createUserAccount = async (client: any, newUser: object) => {
await client.connect().then((res:any) => console.log(res)
// await client.db('onlinestore').collection('user_data').insertOne(newUser).then((result: any) => {
// console.log(result)
// return result
// })
)
}
try {
createUserAccount(client, input)
.then((result) => {return result})
} catch (e) {
console.error(e);
return false
}
// finally {
// await client.close()
// }
}
with the help from everyone here, I was able to solve the issue. It appears that my IP had changed since I had created the Atlas and to fix this I just needed to add my updated IP address. It also appears that a connection timed out error from mongo can be caused by access restrictions, such as username/password or in my case, IP ban from my own service.