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

Wait for mongo connection #304

Closed
fzaffarana opened this issue Jun 13, 2020 · 2 comments
Closed

Wait for mongo connection #304

fzaffarana opened this issue Jun 13, 2020 · 2 comments

Comments

@fzaffarana
Copy link

I realized that the mongoose connect is an async function.

Because that, it's possible that the server start without a established mongo connection. (I could generate errors).

I think in something like that:

exports.connect = async () => {
  await mongoose
    .connect(mongo.uri, {
      useCreateIndex: true,
      keepAlive: 1,
      useNewUrlParser: true,
      useUnifiedTopology: true,
      useFindAndModify: false,
    })
    .then(() => console.log('mongoDB connected...'));
  return mongoose.connection;
};

Or just return mongoose.connect as a promise without then, and await for the resolution of the promise on the other side.

What do you think about that?

@danielfsousa
Copy link
Owner

I think we can just return the value of mongoose.connect, and await it inside a async function (that can be called main or something like this) in https://github.com/danielfsousa/express-rest-es2017-boilerplate/blob/master/src/index.js#L9

@danielfsousa
Copy link
Owner

This will be fixed on version 2 #334

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants