I'm following a tutorial and I'm trying to connect my API to my DataBase on Mongodb.
I add the connection string to my code with .catch and .then in case the connection failed or succeeded.
I don't get any message about connection from the console or in my terminal.
const mongoose = require('mongoose');
mongoose.connect('mongodb srv://<username>:<password>@cluster0.8nytedk.mongodb.net/?retryWrites=true&w=majority',
{ useNewUrlParser: true,
useUnifiedTopology: true })
.then(() => console.log('Connexion à MongoDB réussie !'))
.catch(() => console.log('Connexion à MongoDB échouée !'));
CodePudding user response:
If you've whitelisted your IP address under Security > Network Access > IP Whitelist and then try to connect from a different IP address, Mongo will block the connection. It's a security feature to ensure only trusted IPs can connect to the database. You just need to go to the above mentioned section of the Mongo DB Atlas site and either add your current IP address to the whitelist or temporarily allow access from anywhere (i.e. 0.0.0.0/0 for all IP addresses
).
CodePudding user response:
First thing, make your IP white list on mongoose server.
Kindly share which MongoDB platform you are using.
Next thing print error and share here,
Follow the below approach to print the error. No need to pass .then, .catch
import mongoose from 'mongoose';
mongoose.connect("Your Connection String", (err) => {
if (err) {
console.log(err.message);
return;
}
});