Reputation: 638
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.
Upvotes: 0
Views: 96
Reputation: 632
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.
Upvotes: 1