I am trying to connect my node.js server to the mongo database. I have created a database name "breathe_in_db" in dealer-database.js file and When i try to run the server It says that i have successfully connected to the database on terminal however, no any database named "breathe_in_db" is showing on the mongodb compass.
index.js
//import required dependencies
const express = require('express');
const profiledb = require('./db/dealer-database');
//Initialising express app
const app = express();
//Assigning port
const PORT = process.env.PORT || 3000;
//enable json in request
app.use(express.json())
//Connecting to all essential databases before firing up the API
Promise.all([profiledb.ConnectToDb()])
.then(() => {
console.log("Connected to All Databases")
registerRoutesAndStartListening();
})
.catch((err) => console.log(err));
module.exports = app;
dealer-database.js
const mongoose = require('mongoose');
const DB_NAME = 'breathe_in_db';
const DEFAULT_CONN_STRING = `mongodb://localhost:27017/${ DB_NAME }`
// A Promise to connect TO MongoDB
const ConnectToDb = (connectionString = DEFAULT_CONN_STRING) => {
return new Promise((resolve, reject) => {
mongoose.connect(connectionString, { useNewUrlParser: true, useUnifiedTopology: true },
(err) => {
if (err)
return reject(err)
return resolve();
})
})
}
module.exports = { ConnectToDb };