You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on May 10, 2021. It is now read-only.
Describe the bug
I am using getServerSideProps in a dynamic route to try to get data from my mongoDB database and render it in my page. Locally this works perfectly, but when I deploy to Netlify I get a timeout error:
{
errorMessage: "2021-02-23T18:08:45.948Z ac3c351d-fc95-48ea-9c17-7193e66273b7 Task timed out after 26.03 seconds"
}
NOTE: when testing with an external api there is no issue, so I think this has to be related to MongoDB. MongoDB has IP restrictions but I made sure to add 0.0.0.0/0 so that the database is open to the world, but I still get the error.
Page Code
import { connectToDatabase } from '@/server/lib/connect-database';
function CauseDetail({ data }) {
return (
<div>
{ JSON.stringify(data) }
</div>
);
}
export async function getServerSideProps({ params }) {
const { db } = await connectToDatabase();
const cause = await db
.collection('causes')
.findOne({ handle: params.handle });
const data = JSON.parse(JSON.stringify(cause));
console.log('connected', data);
return { props: { data }};
}
export default CauseDetail;
Connect to Database
import { MongoClient } from 'mongodb';
let uri = process.env.MONGO_CONNECTION;
let dbName = process.env.MONGO_DB;
let cachedClient = null;
let cachedDb = null;
if (!uri) {
throw new Error(
'Please define the MONGO_CONNECTION environment variable inside .env'
)
}
if (!dbName) {
throw new Error(
'Please define the MONGO_DB environment variable inside .env'
)
}
export async function connectToDatabase() {
if (cachedClient && cachedDb) {
return { client: cachedClient, db: cachedDb };
}
const client = await MongoClient.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const db = await client.db(dbName);
cachedClient = client;
cachedDb = db;
return { client, db };
}
Versions
Next: 10.0.7
next-on-netlify: 2.8.7
The text was updated successfully, but these errors were encountered:
hey @djw11192 ! thanks for opening this issue and sorry you're experiencing a function timeout :/ 26 seconds is definitely super long to still be timing out. have you tried putting that db connect and collection.findOne call chain into an api page/route instead? that, or you could try just a pure netlify function - either one of those could maybe help you debug what exactly is hanging/getting stuck. if you can't figure it out that way, you can open a deployable repo that reproduces the issue when deployed and provide me with necessary env variables and then i can take a look. without that right now, i'm not able to do much!
@djw11192 Could you try updating your code to connect to Mongo to the latest recommendation from the Mongo team? We've been working with them to try and improve the Next.js + Mongo experience and prevent connection timeouts in serverless environments.
Also - if possible, try to deploy your Mongo database in us-east to match the location of your Netlify Functions! That will give you the lowest latency to help prevent timeouts 🙌
Describe the bug
I am using
getServerSideProps
in a dynamic route to try to get data from my mongoDB database and render it in my page. Locally this works perfectly, but when I deploy to Netlify I get a timeout error:NOTE: when testing with an external api there is no issue, so I think this has to be related to MongoDB. MongoDB has IP restrictions but I made sure to add
0.0.0.0/0
so that the database is open to the world, but I still get the error.Page Code
Connect to Database
Versions
The text was updated successfully, but these errors were encountered: