We have a site running on Next.js deployed on Vercel, with a MongoDB backend. Currently we have the issue where new users on the site get occasional 502/500 errors when they perform various functionality such as login, signup or viewing items. However, once they refresh the site it works fine again. When I checked the logs on Vercel, the issue appears as follows:
In MongoDB, I have set the Network Access to allow all addresses.
In addition, below is our connectDB function, and an example of where it is called. What might be causing this issue and how could it be resolved? Thanks