I am following this tutorial https://www.youtube.com/watch?v=7CqJlxBYj-M and am having some issues getting my MongoDB to work.
Here is my code so far:
const express = require("express");
const cors = require("cors");
const mongoose = require("mongoose");
require("dotenv").config();
const app = express();
const port = process.env.PORT || 5000;
app.use(cors());
app.use(express.json());
const uri = process.env.ATLAS_URI;
mongoose.connect(uri, { useNewUrlParser: true, useCreateIndex: true });
const connection = mongoose.connection;
connection.once("open", () => {
console.log("MongoDB database connection established successfully!");
});
app.listen(port, () => {
console.log(`Server is running on port: ${port}`);
});
My terminal says:
TypeError: connection.once is not a function
at Object. (C:\Users\Lisa\Desktop\exercise-tracker\mern-exercise-tracker\backend\server.js:16:12)
I think my other problem is this:
In my .env file, I have pasted the ATLAS_URI= connection string from mongodb Atlas and it still isnât working. I did not yet change the part of my .env file. I canât figure out what the name should be! Can anyone help?