0

I have created a cluster on atlas and attempted to connect using my node app and log the connection status with mongoose. I have whitelisted my ip and set everything up properly but I keep getting UnhandledPromiseRejectionWarning.

Here's my code for db.js. Error throws on mongooose.connect(url, opts).

 const mongoose = require('mongoose');

 const db_connect = async () => {
 const conn_string = await mongoose.connect('mongodb+srv://devjoe: 
    <password_hidden_delibarately>@devcamper-gs1nb.mongodb.net/devcamper?retry 
    Writes=true&w=majority', 
   {
        useCreateIndex: true,
        useNewUrlParser: true,
        useFindAndModify: false,
        useUnifiedTopology: true
   }); 

   console.log(`connection string: ${conn_string.connection.host}`);

}

module.exports = db_connect;

In server.js file, I just called the function like db_connect(); after importing with commonjs module.

Any help will be appreciated as I can't find what the issue is. Thanks.

6
  • Hey there, from your question, I just can tell you what in general causes this error: it is generally a promise not handled properly. Can you try this and let me know? db_connect().then(()=>console.log("okay")). catch (()=>console.log(" not okay")) ; Commented Mar 25, 2020 at 18:48
  • What about the connection string, is it not required? Commented Mar 25, 2020 at 19:07
  • It supposes to be at the same place you are calling, except, try to catch the possible errors! Commented Mar 25, 2020 at 19:08
  • The place where you call db_connect(), just replace with the given code and let me know! Commented Mar 25, 2020 at 19:08
  • Hey there, I have tested your code, but using localhost, and it works. maybe the problem is your string. Are you sure it is okay? I myself had several problems with those strings in the beginning. Commented Mar 25, 2020 at 19:20

1 Answer 1

1

You can also try this in case the solution does not work:

const mongoose = require("mongoose");

const db_connect = () => {
  try {
    const conn_string = mongoose.connect(
      "mongodb+srv://devjoe: <*****************>@devcamper-gs1nb.mongodb.net/devcamper?retry Writes=true&w=majority",
      {
        useCreateIndex: true,
        useNewUrlParser: true,
        useFindAndModify: false,
        useUnifiedTopology: true
      }
    );

    console.log(`connection string: ${conn_string.connection.host}`);
  } catch {
    console.log(`not connected to : ${conn_string.connection.host}`);
  }
};

module.exports = db_connect;

I have just tested this solution in my computer, and it works!

But, in case none of that works, can send you how I do the connection.

Sign up to request clarification or add additional context in comments.

2 Comments

It's not connecting either. Error thrown is at this point db_connect in server.js
So, allowing all ips seemed to be the solution. Hmm, wonder what my ip issue was. Thanks for the help though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.