Issue with TypeScript Package for MongoDB and TypeORM MySQL

I'm currently working on a TypeScript package that provides two main functions: one for connecting to a MongoDB database and another for connecting to a TypeORM MySQL database. The package is intended to be reusable across projects.

In my project, I've installed this package and imported the connectToMongo function to establish a connection to my MongoDB database. The connection seems to be established successfully; however, when I attempt to post user data to MongoDB, I encounter the following error:

MongooseError: Operation usermongos.insertOne()buffering timed out after 10000ms

This error occurs even though the MongoDB connection appears to be established without any issues.

I've reviewed my code, and the MongoDB connection configuration appears to be correct. I've also ensured that my network connection to MongoDB is stable. However, I'm still facing this issue.

Could someone please help me understand why I might be encountering this error and how I can resolve it? Any insights or suggestions would be greatly appreciated.

The below is the package

import mongoose  from "mongoose";
import { DataSource } from "typeorm";


mongoose.connection.once("open", () => {
  console.log("Running Mongo");
});

mongoose.connection.on("error", (err) => {
  console.error(`Error in mongo: ${err}`);
});

async function connectToMongo(url: string): Promise<void> {
  await mongoose.connect(url as string);
}

function initializeAppDataSource(generateDataSource: any): DataSource {
  return new DataSource(generateDataSource);
}

async function connectToMySql(generateDataSource: any): Promise<void> {
  const AppDataSource = initializeAppDataSource(generateDataSource);
  try {
    await AppDataSource.initialize();
    console.log("Data Source MySql has been initialized!");
  } catch (err) {
    console.error("Error during Data Source initialization", err);
  }
}

export { connectToMongo, connectToMySql };

Here how i use it in any project

import dotenv from "dotenv";
dotenv.config();
import express from "express";
import { Request, Response } from "express";
import { UserMongo } from "./Entities/User/UserMongo";
import { connectToMongo } from "package1"; // this is the TS package

const app = express();
const PORT = 3000;
app.use(express.json());

app.post(
  "/postuser",
  async (req: Request, res: Response) => {
    try {
      const user = await UserMongo.create(req.body);
      return res.status(201).json(user);
    } catch (error) {
      console.error("Error connecting to MongoDB:", error);
    }
  }
);

async function startServer() {
  try {
    await connectToMongo(process.env.DB_URL as string);
    app.listen(PORT, () => {
      console.log("Running server on mongo");
    });
    //await mongoConnect();
  } catch (error) {
    console.error("Error connecting to MongoDB:", error);
  }
}

startServer();

So when i try to post user i got the error that i mention above but when i connect with Mongoose directly i dont get the error

What I've Tried:

Reviewed my code and ensured that the MongoDB connection configuration is correct. Checked that my network connection to MongoDB is stable. Tried connecting to MongoDB directly using Mongoose and posting data, which works without errors. Expectations:

I expected that when using my TypeScript package to connect to MongoDB and post user data, the operation would complete successfully without errors. However, I'm encountering the mentioned error, and I'm unsure why.

Could someone please help me understand why I might be encountering this error and how I can resolve it? Any insights or suggestions would be greatly appreciated.

This question provides a clear description of the issue, what you've tried, and your expectations for the outcome. It should help others on the platform understand your problem and provide effective assistance.

0

There are 0 best solutions below