Mongo DB remote connection error not able to load data from DB

I have created mongodb database on AWS and trying to connect with the stage environment. but getting below error. All configuration settings are in place app is running. The only issue with load data from the MongoDB server.

> OPTIONS /forest/authentication/callback?code=nD9M5TbF65weexquJgjvVWDDFyz0Sa4wUCop3COWKdOgCDmmcUcQRy5N5oJQTMn-&state=%7B%22renderingId%22%3A93941%7D 204 0 - 1.273 ms
> GET /forest/authentication/callback?code=nD9M5TbF65weexquJgjvVWDDFyz0Sa4wUCop3COWKdOgCDmmcUcQRy5N5oJQTMn-&state=%7B%22renderingId%22%3A93941%7D 200 536 - 1677.196 ms
> (node:28486) UnhandledPromiseRejectionWarning: MongooseTimeoutError: Server selection timed out after 30000 ms

I have connected that same database from another node app it is working fine. but issue only with the forest admin. Is there any additional setting needed?

We are accessing the database via IP with auth.

Thanks in advance :pray:

Hello @sourabht, :wave:

Let’s try to find the culprit. So I have some questions:

  • You say stage environment, what do you mean ? A remote staging environment ?
  • Which version of forest-express-mongoose are you using ?
  • Have you updated the whitelist on AWS with this “stage environment” IP addresses ?

Kind regards,
Morgan

Hi @morganperre ,

Sure I can give you an answer to your questions.

#1. We have created a remote environment and it is up n run.
#2. forest-express-mongoose - ^7.0.0
#3. Yes, we have whitelisted the IP of the stage environment.

Thanks & Regards,
Sourabh

Hi @sourabht :wave: Have you run MongoDB in Ec2 AWS instance or you use DynamoDB?

Hi @Arnaud_Moncel ,

We are running mongoDB on AWS EC2 instance.

Thanks,
Sourabh

Have you double check your DATABASE_URL env var?

Yes here is the example url which we have configured.

“mongodb://{username}:{password}@{host as ip}:{port}/{dbname}”

here we have set host is IP.

Can you share with us the options passed to the Mongoose.createConnection(url, options) function please :pray:

Yes sure, Please find below the function.

databasesConfiguration.forEach((databaseInfo) => {
  const connection = Mongoose.createConnection(databaseInfo.connection.url, databaseInfo.connection.options);
  connections[databaseInfo.name] = connection;

  const modelsDir = databaseInfo.modelsDir || path.join(__dirname, databaseInfo.name);
  fs
    .readdirSync(modelsDir)
    .filter((file) => file.indexOf('.') !== 0 && file !== 'index.js')
    .forEach((file) => {
      try {
        const model = require(path.join(modelsDir, file))(connection, Mongoose);
        db[model.modelName] = model;
      } catch (error) {
        console.error(`Model creation error: ${error}`);
      }
    });
});

db.objectMapping = Mongoose;
db.connections = connections;

module.exports = db;

I want to know more precisely this value.

const databaseOptions = {
  useNewUrlParser: true,
  useUnifiedTopology: true,
};

module.exports = [{
  name: 'default',
  modelsDir: path.resolve(__dirname, '../models'),
  connection: {
    url: process.env.DATABASE_URL,
    options: { ...databaseOptions },
  },
}];

above is the code pass to options

Can you try to have a look on this documentation Mongoose v5.12.14: Connecting to MongoDB and get the reason of the timed out?

@Arnaud_Moncel thanks for your support. but still getting issues connecting database.

(node:6093) UnhandledPromiseRejectionWarning: MongooseTimeoutError: Server selection timed out after 50000 ms

Reason:

(node:6093) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:6093) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

The reason you give me is not the one we’re looking for.
The log you shared mean an error is throw on a async process and not handled.
How do you get this error?

Hello @sourabht :wave:

It looks like the connection to your remote MongoDB server times out. This could be for several reasons.

You said previously that other applications successful connect to your MongoDB server. Here come some questions around that:

  • Are those correctly connected applications in the same VPC as your database ?
  • Is the Staging environment in the same VPC as the database ?
  • Do you use a particular authSource to connect ?
  • Did you compare the mongodb connection URI across applications to try to identify differences ?

Thanks in advance for your response,

Steve.

Hello @Steve_Bunlon ,

Thanks, below are answers to your query.

  • Are those correctly connected applications in the same VPC as your database ? Yes
  • Is the Staging environment in the same VPC as the database ? Yes
  • Do you use a particular authSource to connect ? No
  • Did you compare the mongodb connection URI across applications to try to identify differences ? Yes here we have another app which is connected with the same MongoDB database and it is working fine.

Thanks,
Sourabh