0

I am establishing connection from a seperate registered npm package that I have created, But for some reason I'm getting this error:

MongooseError: Operation `users.findOne()` buffering timed out after 10000ms
    at Timeout.<anonymous> (C:\Users\Lior\Desktop\Work\kbrain-central\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:175:23)
    at listOnTimeout (node:internal/timers:569:17)
    at processTimers (node:internal/timers:512:7)

My init connection function in the package:

const init = async () => {
  try {
    // fix bug "Retryable writes are not supported" in documentDb (https://stackoverflow.com/questions/65453371/mongoose-findoneandupdate-throw-retryable-writes-are-not-suppMongoose%20findOneAndUpdate%20throw%20%22Retryable%20writes%20are%20not%20supported%22rted-error-when)
    await mongoose.connect(DB_URL, {
      ssl: SSL_BOOLEAN,
      retryWrites: false,
    });
    const db = mongoose.connection;
    db.on("error", (err) => logger.logError(err));
    db.on("disconnected", () => logger.logError("Lost MongoDB connection"));
    console.log("Connection established successfully")
  } catch (error) {
    throw new DatabaseConnectionError();
  }
};

My main app code:

import { init as initDB } from "@phaedra/db";
import { app } from "./app";

async function start() {
  await initDB();
  app.listen(process.env.PORT || 3000, () => {
    console.log("Auth service up");
  });
}

start();

When I'm using the same code but not imported (just copy it), it works just fine.

Edit: Tried isolating the connection function and do a simple query and still doesn't work.

Lior Shein
  • 11
  • 3
  • Isn't there a sideeffect triggering `findOne` somewhere while the initDB is not done yet, i see that it is awaited but maybe some IIFE – Lars Vonk Mar 08 '23 at 09:14
  • @LarsVonk the connection seems to be established, getting the "Connection established successfully" message after the await mongoose.connect(). – Lior Shein Mar 08 '23 at 09:19
  • but is there not a call happening before it is connecting succesfully? – Lars Vonk Mar 08 '23 at 15:48

0 Answers0