Skip to content
This repository has been archived by the owner on May 10, 2021. It is now read-only.

Timeout error when using MongoDB in getServerSideProps #172

Closed
djw11192 opened this issue Feb 23, 2021 · 4 comments
Closed

Timeout error when using MongoDB in getServerSideProps #172

djw11192 opened this issue Feb 23, 2021 · 4 comments
Labels
mongo related to mongo priority: medium type: bug code to address defects in shipped code

Comments

@djw11192
Copy link

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
@lindsaylevine lindsaylevine added type: bug code to address defects in shipped code priority: medium labels Feb 24, 2021
@lindsaylevine
Copy link
Contributor

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!

@lindsaylevine lindsaylevine added the mongo related to mongo label Feb 25, 2021
@leerob
Copy link

leerob commented Feb 25, 2021

@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 🙌

@lindsaylevine please correct me if anything is wrong 😄

@lindsaylevine
Copy link
Contributor

@djw11192 hey! any updates here? :D

@lindsaylevine
Copy link
Contributor

closing due to inactivity; feel free to reopen!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
mongo related to mongo priority: medium type: bug code to address defects in shipped code
Projects
None yet
Development

No branches or pull requests

3 participants