Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot connect to mongo db when node version > 17 #11250

Closed
daniloab opened this issue Jan 19, 2022 · 4 comments
Closed

cannot connect to mongo db when node version > 17 #11250

daniloab opened this issue Jan 19, 2022 · 4 comments
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@daniloab
Copy link

when using node 17 my application cannot connect to MongoDB

script of example

import mongoose from 'mongoose';

import { MONGO_URI } from './config';

const mongooseOptions = {
  // autoIndex: true,
};

export const connectDatabase = () =>
  new Promise((resolve, reject) => {
    mongoose.connection
      // Reject if an error ocurred when trying to connect to MongoDB
      .on('error', (error) => {
        // eslint-disable-next-line
        console.log('ERROR: Connection to MongoDB failed');
        reject(error);
      })
      // Exit Process if there is no longer a Database Connection
      .on('close', () => {
        // eslint-disable-next-line
        console.log('ERROR: Connection to MongoDB lost');
        process.exit(1);
      })
      // Connected to DB
      .once('open', () => {
        // Display connection information
        const infos = mongoose.connections;
        // eslint-disable-next-line
        infos.map((info) =>
          console.log(`Connected to ${info.host}:${info.port}/${info.name}`),
        );
        // Return sucessfull promisse
        resolve();
      });

    mongoose.connect(MONGO_URI, mongooseOptions);
  });

running this simple connection with your MONGO_URI should return the error below:

Screen Shot 2022-01-19 at 13 16 11

For some reason, my environment was on node 17. When I come back to the LTS version 16 returns to works normally.

@IslandRhythms
Copy link
Collaborator

import * as mongoose from 'mongoose';


mongoose.connection.on('connected', () => {
    console.log('connected')
});

mongoose.connection.on('error', () => {
    console.log('error')
})


async function run() {
await mongoose.connect('mongodb://localhost:27017');
console.log('done');
}

run();

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Jan 19, 2022
@sean-daley
Copy link

sean-daley commented Jan 20, 2022

#11066

Looks like the same problem reported there?

Also this seems to be a new intended behavior in node 17 I think.
nodejs/node#40537

@vkarpov15 vkarpov15 added this to the 6.1.9 milestone Jan 23, 2022
@vkarpov15
Copy link
Collaborator

@sean-daley is right, #11066 (comment) is the answer. 3 options:

  1. Use 127.0.0.1 instead of localhost
  2. Modify your /etc/hosts and make sure ::1 localhost is after 127.0.0.1 localhost
  3. Start the MongoDB server with ipv6 support. For example, by setting --ipv6 when starting MongoDB server on the command line

Looks like Node v17 changed up the resolution order in nodejs/node#40537, which is why this started happening for you in node v17.

@vkarpov15 vkarpov15 removed this from the 6.2.2 milestone Feb 11, 2022
@vkarpov15 vkarpov15 added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary and removed confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. labels Feb 11, 2022
@daniloab
Copy link
Author

tks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

4 participants