2f8c729d-7a3c-4781-b43f-1732e25e75ac Task timed out after 10.01 seconds, when using MongoDB

On my page: ‘admin-sokol.netlify.app’ I am always getting this error when loading the site. I tracedback the error to a line of code which connects me to my MongoDB database. When connecting from localhost, no errors are given, but if I try to connect through my site on Netlify, it just won’t connect. How can I prevent this error?

This is my code:

import { MongoClient} from "mongodb";
import { DB_URI } from "$env/static/private";

const client = new MongoClient(DB_URI)

async function startMongo() {
    console.log("Starting MongoDB..")

    await client.connect()
}

startMongo().then(r => {
    console.log("Successfully connected to the database.")
})

Hi thanks for reaching out. You can prevent this error by upgrading to the pro plan and having your function timeout increased so your function does not run out of time. The default limit for starter plans is 10 seconds

:)) Not sure why are you suggesting to buy something from you that I don’t need.
I found a solution by myself - not sure why it works, but when I put “0.0.0.0/0” in ‘Network Access’ in MongoDB it works.

Thanks.