Unhandled Promise Rejection Errors on Vercel (MongoDB) #4165
Replies: 11 comments 17 replies
-
Are you sure you can connect to the database itself? I.e. all IP adresses can access the database? |
Beta Was this translation helpful? Give feedback.
-
UPDATE: This seems to have fixed my issue. I will monitor my app and mark this question as answered after a day or so to ensure the issue is truly fixed. |
Beta Was this translation helpful? Give feedback.
-
I'm having the same problem, but I'm using a Mongo Atlas shared tier cluster (M0), which is free and not suitable for production environments. Are you using a similar cluster for low-traffic applications? |
Beta Was this translation helpful? Give feedback.
-
Hi there, I just wanted to comment on this thread again since I am still dealing with this issue. Hoping someone will come across this thread and have a solution. I have continued trying different things to get rid of the MongoServerSelectionError. What I believe is happening is that the database is closing the connection to the frontend app due to idle time, however, I don't think the app is aware that the connection is closed, so the next time it tries to send a request to the database, the request fails and my app returns a 500 error. I have tried including multiple different options in the connection string in attempts to fine tune the connection availability. https://www.mongodb.com/docs/manual/reference/connection-string/ My current set up is as follows:
|
Beta Was this translation helpful? Give feedback.
-
Also, here is the current connection string options that I am providing: ssl=true&replicaSet=globaldb&retrywrites=false&maxIdleTimeMS=120000 These are the default options in the connection string provided by Azure. I have also tried many different option configurations, but am open for suggestions. |
Beta Was this translation helpful? Give feedback.
-
I'm facing the same issue. Did anyone find success in fixing it? |
Beta Was this translation helpful? Give feedback.
-
I'm fighting this same error? Anyone figure this out? I'm deployed on Vercel, using mongo and next auth. |
Beta Was this translation helpful? Give feedback.
-
Hi, i'm also facing this with atlas and mongodb in vercel deployment. It's not a constant error. It happens time to time. I'm using the app router and the next-auth@v5. client.js: // This approach is taken from https://github.com/vercel/next.js/tree/canary/examples/with-mongodb
import { MongoClient } from "mongodb";
if (!process.env.MONGODB_URI) {
throw new Error('Invalid/Missing environment variable: "MONGODB_URI"');
}
const uri = process.env.MONGODB_URI;
const options = {};
let client;
let clientPromise;
if (process.env.NODE_ENV === "development") {
// In development mode, use a global variable so that the value
// is preserved across module reloads caused by HMR (Hot Module Replacement).
if (!global._mongoClientPromise) {
client = new MongoClient(uri, options);
global._mongoClientPromise = client.connect();
}
clientPromise = global._mongoClientPromise;
} else {
client = new MongoClient(uri, options);
clientPromise = client.connect();
}
export default clientPromise; auth.js import NextAuth from "next-auth";
import { MongoDBAdapter } from "@auth/mongodb-adapter";
import GitHub from "next-auth/providers/github";
import clientPromise from "@/db/client";
export const {
handlers: { GET, POST },
auth,
} = NextAuth({
providers: [
GitHub({
clientId: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET,
}),
],
secret: process.env.NEXTAUTH_SECRET,
adapter: MongoDBAdapter(clientPromise),
session: { strategy: "jwt" },
});
export const withAuth = (handler) =>
auth((req, ...rest) => {
if (!req.auth) {
return new Response(null, { status: 403 });
}
return handler(req, ...rest);
}); And the error message:
And it happens time to time when the next-auth hits the /api/auth/session endpoint. |
Beta Was this translation helpful? Give feedback.
-
Facing the same issue. I've tried multiple ways of deploying:
Database is a Managed Database from Digitalocean. I've also tried a paid Mongodb atlas M2 (9$ p/m) mongodb. Same error. Issue persists. Would love to hear a solution... |
Beta Was this translation helpful? Give feedback.
-
In the meantime i figured out something. After a while i revisited my project and tought what if, we put the whole logic into a function. I tested and the error dissappeared for me: import { MongoClient } from "mongodb";
if (!process.env.MONGODB_URI) {
throw new Error('Invalid/Missing environment variable: "MONGODB_URI"');
}
const uri = process.env.MONGODB_URI;
const options = {};
let client;
let clientPromise;
function getClientPromise() {
if (process.env.NODE_ENV === "development") {
// In development mode, use a global variable so that the value
// is preserved across module reloads caused by HMR (Hot Module Replacement).
if (!global._mongoClientPromise) {
client = new MongoClient(uri, options);
global._mongoClientPromise = client.connect();
}
clientPromise = global._mongoClientPromise;
} else {
client = new MongoClient(uri, options);
clientPromise = client.connect();
}
return clientPromise;
}
export default getClientPromise; Any other suggestion? Maybe it will ends with to many open connection? |
Beta Was this translation helpful? Give feedback.
-
+1. I'm using the exact same setup as @dubbajubba (see comment). The error is always the same:
My current code is:
Did someone manage to solve this issue? I'm quite desperate, have been trying to solve this for almost one month (also in other threads) without success, and it's blocking my app's launch :( Many thanks! |
Beta Was this translation helpful? Give feedback.
-
Hi there,
I'm hoping someone can shed some light on my issue. I have been trying to figure this out and I am going crazy!
Problem
I'm running my nextjs app on Vercel. Next-auth is working great for the most part, but I have consistently been getting Unhandled Promise Rejection errors in the Vercel function logs when hitting the next-auth api.
Below is a screenshot of the errors I am getting.
I am using a MongoDB that I have set up with Azure's Cosmo DB. Below is my code that might be relevant.
/lib/mongodb.ts (I used the code from the next-auth documentation, but made some tweaks to work with typescript)
/pages/api/auth/[...nextauth.ts]
getServerSideProps from /pages/signin.tsx (i can provide more if necessary)
I had to update the next.d.ts file and add a declaration for global with the _mongoClientPromise to fix typescript errors
/types/next-auth.d.ts
P.S. please let me know if I need to provide anything else!
Beta Was this translation helpful? Give feedback.
All reactions