504 Gateway Timeout, already tried everything I could
Answered
Munchkin posted this in #help-forum
MunchkinOP
Hi there, I'm trying to deploy my NextJS with Vercel. The project works fine locally but when I try to deploy it I got a
here's what I already did :
- I already tried changing the function max direction to 30 seconds but mongoose still has it timed out after 10 seconds and it only changes the error from 504 to 500, its said that
- I already added 0.0.0.0/0 to MongoDB atlas network access
- I already set my environment variables on my staging deployment (this is my staging deployment)
- On locally this query only takes 432 ms so the query I'm running is supposed to work just fine
- It was written on the console log while building that the connection has been established
Im using :
- NextJS
- MongoDB Atlas
- Mongoose
I require your help 🙏
504 gateway timeout error.
here's what I already did :
- I already tried changing the function max direction to 30 seconds but mongoose still has it timed out after 10 seconds and it only changes the error from 504 to 500, its said that
"users.findOne() buffering timed out after 10000 ms"
- I already added 0.0.0.0/0 to MongoDB atlas network access
- I already set my environment variables on my staging deployment (this is my staging deployment)
- On locally this query only takes 432 ms so the query I'm running is supposed to work just fine
- It was written on the console log while building that the connection has been established
Im using :
- NextJS
- MongoDB Atlas
- Mongoose
I require your help 🙏
Answered by B33fb0n3
yay, so to conclude everything:
- Use await to "await" the connection building process
- Create a new connection on every route
- Use await to "await" the connection building process
- Create a new connection on every route
32 Replies
@Munchkin Hi there, I'm trying to deploy my NextJS with Vercel. The project works fine locally but when I try to deploy it I got a `504 gateway timeout error.`
here's what I already did :
- I already tried changing the function max direction to 30 seconds but mongoose still has it timed out after 10 seconds and it only changes the error from 504 to 500, its said that `"users.findOne() buffering timed out after 10000 ms"`
- I already added 0.0.0.0/0 to MongoDB atlas network access
- I already set my environment variables on my staging deployment *(this is my staging deployment)*
- On locally this query only takes 432 ms so the query I'm running is supposed to work just fine
- It was written on the console log while building that the connection has been established
Im using :
- NextJS
- MongoDB Atlas
- Mongoose
I require your help 🙏
it looks like your connection timed out. Make sure that you placed your DB credentials correctly in production (for example add them to your hosters website, as the .env.local won't be pushed to github). Also make sure that you have an active connection to your database.
If you checked both, tell me how you checked it and answer the questions
If you checked both, tell me how you checked it and answer the questions
@B33fb0n3 it looks like your connection timed out. Make sure that you placed your DB credentials correctly in production (for example add them to your hosters website, as the .env.local won't be pushed to github). Also make sure that you have an active connection to your database.
If you checked both, tell me how you checked it and answer the questions
MunchkinOP
I already uploaded my env files to my environment on that project
if my DB credentials were incorrect it should stopped while building, since that error already happened before
here said that the connection as has been established
@Munchkin here said that the connection as has been established
how do you "cache" your DB connection?
@B33fb0n3 how do you "cache" your DB connection?
MunchkinOP
I'll give you my code for the db connection in a minute hold on
MunchkinOP
export async function connectToMongoDB(){
if (cached) {
console.log("Using cached DB connection")
return cached;
}
try{
const cnt = await mongoose.connect(DATABASE_URL!)
cached = cnt.connection;
console.log("New connection has been established");
return cached;
} catch (error){
console.log(error);
throw error;
}
}
@B33fb0n3 so if it already established a connection before it'll use that
@B33fb0n3 can you enable fluid compute and try again?
MunchkinOP
what does fluid compute does?
ill try
but i want to know what it does exactly lol
@Munchkin but i want to know what it does exactly lol
it makes your functions be able to run longer (simplified)
@B33fb0n3 it makes your functions be able to run longer (simplified)
MunchkinOP
yeah its still timing out :")
its a simple login
@Munchkin Click to see attachment
are you sure you enabled it and redeployed it? Normally the timeout should be then 60000ms
@B33fb0n3 are you sure you enabled it and redeployed it? Normally the timeout should be then 60000ms
MunchkinOP
yeah i did redeploy it, seems like the error isnt coming from vercel but mongoose
and it shouldnt be taking 10 seconds
in the first place
GOD
i found a temporary fix
i added connectToDB in my routes and apparently that works on the login route
but surely i dont need to do that right?
my friend's project works fine without this
@Munchkin Click to see attachment
yea, normally importing should work. But without any await? 🤔
let me add await in the main one, actually maybe i am stupid
MunchkinOP
@B33fb0n3 so i added the await but i just resort to adding connect to mongoDB in every route and it seems to be working :)
yay, so to conclude everything:
- Use await to "await" the connection building process
- Create a new connection on every route
- Use await to "await" the connection building process
- Create a new connection on every route
Answer