This is my app.js file.
require("dotenv").config()
// async errors
const express = require("express");
const app = express();
const connectDB = require("./db/connect")
const notFoundMiddleware = require("./middleware/not-found");
const errorMiddleware = require("./middleware/error-handler");
// middleware
app.use(express.json())
// routes
app.get("/", (req,res) => {
res.send('<h1>Store API</h1><a href="/api/v1/products">products route</a>')
})
// products route
app.use(notFoundMiddleware)
app.use(errorMiddleware)
const port = 5000
const start = async () => {
try {
// connectDB
await connectDB(process.env.MONGO_URI)
app.listen(port, console.log(`Server is listening on port ${port}..`))
} catch (error) {
console.log(error);
}
}
start();
This is my connect.js file.
const mongoose = require('mongoose')
const connectDB = (url) => {
return mongoose.connect(url, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true,
})
}
module.exports = connectDB
So, I want to access my DB on MongoDB. I created a .env file and wrote there my MongoDB application code. Then I want to process it on my app .js but when I wrote and ran this code await connectDB(process.env.MONGO_URI) it returned back an authentication error.
How can I fix this error?
to access mongodb try this way
for mongodb connection
No need to use