79264147

Date: 2024-12-09 07:33:55
Score: 0.5
Natty:
Report link

try creating /db.js file with following connection function

const mongoose=require('mongoose')
async function connectMongodb(url){
    return mongoose.connect(url)
    .then(() => console.log(new Date().toISOString()+": Mongodb connected"))
    .catch(err => console.log("error", err))
}

module.exports ={
    connectMongodb,
} 

and in your server.js/index.js file import and run funtion with suitable mongo uri in following way

 const newLocal = `./db`;
const { connectMongodb } = require(newLocal);
//establishin db connections
connectMongodb(process.env.MongoUrl)

in .env add MongoUrl="mongodb://root:[email protected]:27017/NextBase?authSource={your collection name}"

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Low reputation (1):
Posted by: Deekshant