Mongodb atlas works locally but not on vercel with express.js

This endpoint should return {“data“:[…]}

/api/hotels

https://easy-bookings-backend.vercel.app/api/hotels

If you’re having trouble deploying an Express app, this guide can help.

You can also ask v0 for suggestions tailored to your own project setup.

Hi @rawdaymohamed-8924, welcome to the Vercel Community!

What is the error that you get?

{"message":"Operation `hotels.find()` buffering timed out after 10000ms","stack":"MongooseError: Operation `hotels.find()` buffering timed out after 10000ms\n    at Timeout.<anonymous> (/var/task/backend/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:187:23)\n    at listOnTimeout (node:internal/timers:588:17)\n    at process.processTimers (node:internal/timers:523:7)","raw":{}}

This is the controller

export const getAll = async (req, res, next) => {

try {

const hotels = await Hotel.find({});

return res.status(201).json({ data: hotels });

  } catch (error) {

console.log("error:", error);

// return res.status(500).json({ error: error });

return res.status(500).json({

message: error.message,

stack: error.stack,

raw: error,

    });

// return next(createError("Can't get hotels", 500));

  }

};
import mongoose from "mongoose";
import dotenv from "dotenv";
dotenv.config();

export async function connectDb(uri) {
  if (!uri) throw new Error("Missing MongoDB URI (MONGODB_URI)");
  await mongoose.connect(uri);
  console.log("Database connection successfully to MongoDB");
}

export async function disconnectDb() {
  if (mongoose.connection.readyState !== 0) {
    await mongoose.connection.close();
  }
}

This is db connecton

Hi there, is the connection URL correct? Is the connection working? I don’t see the code where you call the connectDb function. It seems like either of the two options:

  1. The connection URI is incorrect. Maybe missing the password or something.
  2. The Atlas cluster is not accepting connections from outside/internet.

Atlas by default blocks all traffic except to whitelisted IP addresses

You can set up your cluster security so it’s protected by username/password (which will be in your connection string) and then disable the IP whitelist requirement and that will allow the connection

(we do also offer static IPs but they’re a paid addon at $100/m Static IPs)

1 Like

No the url is correct this is what I get from using that same url on localhost

mongodb+srv://username:pwd@cluster0.hogwt.mongodb.net/easybookings?retryWrites=true&w=majority

image

I set IP address to all

Hi , I am also getting the same issue even after setting correct URL and IP address in mongoDB to 0.0.0.0/0. It is working correctly on localhost.

Hi @rawdaymohamed-8924, thanks for confirming. Have you tried using this same connection URL at any other service? I think it is related to the network issue as Jacob said.

To verify my claim, I made a sample repo with a new database on Mongo Atlas:

When I remove this from the allowlist, I get an error.

After re-adding it and waiting for 1 minute, all future queriers succeed, which further confirms that this is not a Vercel issue.

Please ensure the IP allowlist is correct and the database connection string has the correct credentials.

I added the same as you did but still not working

Okay. Can you try using the mongodb code I used in the repo shared above? Maybe it’s a drivers issue.

Can you show me the code for mongoose and javascript. This code uses plain mongodb

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.