Netlify function timeout

Kindly visit the link below on how to install npm packages in Netlify Functions.
https://flaviocopes.com/netlify-functions-npm-packages/

In your case you install mongodb using the command below.

npm install mongodb

Thanks.

Thanks for your answer!
I actually have a package.json in the root of my angular project.
“mongodb” package is already install in it.

Should I initialize a new package.json inside netlify/functions folder?

Kindly install mongodb again in the root of your project.

Also follow the tutorial below to learn how to install packages in server-less functions.

Thanks.

:confused: just created a dedicated package.json
image

But I’m still having the same issue regarding mongodb :confused:

Hi @imad, remove the node_modules folder in the functions directory, package.json and package-lock.json.

Kindly visit the official mongodb website below on how to install mongodb with Netlify Functions.

Thanks.

HI @imad, note that there are some limitations on function execution, with the 10 second execution limit being one of the most critical factors.
If the operations in your functions takes more than 10 seconds to complete you will still experience the function crash.

Thanks for your reply.
Actually the function only gets an empty array from a mongoDB database. I’m not sure if this action can take more then 10 seconds…

Can you help me debugging it

The quoted message above could be the reason why it is failing.
My suggestion is to kindly try it out your functions locally on your computer and then time the operations happening to see if the root cause could be from where your MongoDB instance is located or not.

Let me know the outcome.

Thanks.

In addition to the above kindly refactor your code to the code below. Looks like your mongodb connection is different from the one from the official documentation.

const { MongoClient } = require('mongodb');
require('dotenv').config();

const mongoClient = new MongoClient(process.env.MONGODB_URI);

export async function handler(event, context) {
  try {
    console.time('doStuff');

    const database = mongoClient.db(process.env.MONGODB_DATABASE);
   
    const collection = database.collection(process.env.MONGODB_COLLECTION);

    const result = await collection.find({}).limit(10).toArray();

    console.timeEnd('doStuff');

    return {
      body: JSON.stringify(result),
      statusCode: 200,
    };
  } catch (error) {
    return {
      statusCode: 500,
      body: JSON.stringify(error),
    };
  }
}

You can learn more about how to properly connect to mongodb at https://www.mongodb.com/docs/drivers/node/current/quick-start/connect-to-mongodb/#create-your-node.js-application

Let me know the outcome.

Thanks.

Thanks for your answer.

I replaced my code by the one you provide but I’m still having a timeout on mongoDB connection.
If I return a string inside the function everything is ok, but when I add the part regarding the connection to mongo, It’s still failing with a timeout after 26 seconds.

I’m not sure that I missed something for the connection:
DATABASE URI
DATABASE NAME
COLLECTION

Knowing that when I copy paste the URI on Compass, It’s working fine

Hi @imad, thanks for the feedback.
It is possible your environment variables are not loading.

Kindly console log all your environment variables to confirm if it is being loaded.
Thanks.

That’s what I thought :slight_smile:
So I put the URI directly in the method & still having the timeout

Have you also console logged all the variables below?

  1. process.env.MONGODB_DATABASE

  2. process.env.MONGODB_COLLECTION

If possible kindly type the variables in the code directly to see if it works.
Also confirm the database name and collection name again to be sure.

Thanks.

Yes It’s sending the correct data. It’s a very weird timeout. There is no info

Thank you!

Kinldy confirm if the function has stopped crashing after the suggestions.
If your problem has been resolved kindly mark the post as resolved in order to helps others in the future who might experience the same problem.

Thanks.

It’s not solved yet.
I think that I’m going to change the database. It looks like it’s a problem with Mongo…
Do you know any DB which works fine with Netlify functions?

Hi @imad, I suggest you try out mongoose to see if it works.
https://mongoosejs.com/

If you try mongoose and you still have issues with Mongodb, alternatively you can try out PlanetScale.
PlanetScale Integrates well with Netlify.

Others include

  1. DataStax | Netlify Integrations

  2. Fauna | Netlify Integrations

Thanks.

Thanks for your answers!
I’ll try with planetscale. But It’s very weird that It’s not working with mongoDB, It’s a blocking point regarding the people who are used to maintain a db using mongo…

I’m having the same issue with planetScale…
The connection is never etablished

Hi @imad, kindly share any error messages from your logs if possible.