Can't connect to MongoDB

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?

Maybe I should use this code from MongoDB


const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://lisaGodenick:<password>@cluster0.ybrar.mongodb.net/<dbname>?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
  const collection = client.db("test").collection("devices");
  // perform actions on the collection object
  client.close();
});

And put in the correct password, obviously.

Hello @lisagodenick
You have to replace <dbname> and <password> in ATLAS_URI with the db name and password you set for your database in mongodb atlas.

Thanks! This may sound really silly… but I don’t know where to find my db name. Would that be my “project name” in mongodb atlas? Which in this case, I named Exercise App.

Your first example is Mongoose, and your second example is MongoDB. If the tutorial you are following is using Mongoose then you should stick with that. The syntax appears to be correct. Make sure you installed it properly according to the tutorial.

<dbname> can be whatever you want. “test” is a common name used for testing.

Thanks. I have that in there I think it is working now…

Nothing’s silly when it comes to learning and asking questions.

  1. Go to your mongo atlas homepage.
  2. Click the ‘Database Access’ tab under security section in the left sidebar
  3. There you’ll see a button “ADD A NEW DATABASE USER”. Click on it, fill in the username and password fields and create user.
  4. The username is now going to replace <dbname> and password you have set is going to replace <password> in ATLAS_URI.
  5. Now go to the “Network Access” tab and click ‘add new ip’ button. Use 0.0.0.0/0 as the ip as it’s allows access to everyone.
  6. You are now good to go with the setup.

Thank you so very much!!! It worked. When I looked in my terminal and read the errors, not having the correct IP was part of my problem.

1 Like

That’s good. Good luck with your learning journey!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.