BeastDevMoney
BeastDevMoney

Reputation: 1

TimeOut Error: Problems with MongoDB Atlas configuration in Production Environment (Vercel)

I’m having TimeOut errors when I try to save or find out something in the collections of the clusters. Here is an example of the log:

Error en authorize:  MongooseError: Operation `users_profiles.findOne()` buffering timed out after 10000ms
at Timeout.<anonymous> (/var/task/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:185:23)
at listOnTimeout (node:internal/timers:569:17)
at process.processTimers (node:internal/timers:512:7)

The key thing is that it doesn’t happen always on every operation, it misses the half of them or it happens in random time interval.

The project is built using the NextJS pages router, and the configuration of the database connections has the NextJS structure:

/database/

/schemas/

/Connections.ts/ the connection function that returns two promises for two database connections.

/models.ts/ the assignment of each scheme to each connection

connections.ts:

import mongoose from "mongoose";
export const runtime = "nodejs";

interface DatabaseConnections {
  mvpDB: mongoose.Connection;
  chatbotDB: mongoose.Connection;
}


let cached: { conn: DatabaseConnections | null; promise: Promise<DatabaseConnections>  | null } = global.mongoose;

if (!cached) {
  cached = global.mongoose = { conn: null, promise: null };
}

export const connectDBs = async () => {
  const MVP_URI = process.env.DATABASE_SECRET_MVP;
  const CHATBOT_URI = process.env.DATABASE_SECRET_CHATBOT;

  if (!MVP_URI || !CHATBOT_URI) {
    throw new Error("MongoDB URIs not found in environment variables");
  }

  if (cached.conn) {
    return cached.conn;
  }

  if (!cached.promise) {
    const opts = {
      bufferCommands: true,
      minPoolSize: 2,
      maxPoolSize: 4,
      serverSelectionTimeoutMS: 5000,
      socketTimeoutMS: 45000,
      connectTimeoutMS: 10000,
      maxIdleTimeMS: 270000,
      retryWrites: true,
      retryReads: true
    };

cached.promise = (async () => {
  try {
    const mvpDB = await mongoose.createConnection(MVP_URI, opts);
    const chatbotDB = await mongoose.createConnection(CHATBOT_URI, opts);

    mvpDB.on('error', (error) => {
      console.error('MVP DB connection error:', error);
      cached.conn = null;
      cached.promise = null;
    });

    chatbotDB.on('error', (error) => {
      console.error('Chatbot DB connection error:', error);
      cached.conn = null;
      cached.promise = null;
    });

    return { mvpDB, chatbotDB };
  } catch (error) {
    cached.promise = null;
    throw error;
  }
})();
  }

  try {
    cached.conn = await cached.promise;
    return cached.conn;
  } catch (error) {
    cached.promise = null;
    throw error;
  }
};

model.ts

import mongoose from "mongoose"
import { connectDBs } from "./dbNewType";
import { user } from "./schemas/userschema_app";
import { conversationSchema } from "./schemas/conversations";
import { dietSchema } from "./schemas/DietSchema";
import { users_answers } from "./schemas/UserAnswers";
import { customerSchema } from "./schemas/customerSchema";
import { subscriptionSchema } from "./schemas/subscriptionSchema";

const { mvpDB, chatbotDB } = await connectDBs();

// Check if models exist before defining them
export const Users_Profile = mvpDB.models.users_profiles || mvpDB.model('users_profiles', user);
export const Users_Answers = mvpDB.models.users_answers || mvpDB.model('users_answers', users_answers);
export const Diet = mvpDB.models.Diet || mvpDB.model('Diet', dietSchema);
export const Customer = mvpDB.models.Customer || mvpDB.model('Customer', customerSchema);
export const Subscription = mvpDB.models.Subscription || mvpDB.model('Subscription', subscriptionSchema);
export const Conversation = chatbotDB.models.Conversation || chatbotDB.model('Conversation', conversationSchema);


export default {
    Users_Profile,
    Users_Answers,
    Diet,
    Conversation, 
    Customer,
    Subscription 
}

Then when I want to use any model, I import and then invoke the method users_profiles.findOne().

Upvotes: 0

Views: 37

Answers (0)

Related Questions