MongoDB and Mongoose - Install and Set Up Mongoose

I’m using VSCode, and installed Mongoose and MongoDB

npm install mongodb
npm install mongooose

Created .env file and add the URI

Reading the doc, the only way I can connect to the dabase is with the following code

const { MongoClient } = require("mongoose");
MongoClient.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true })

When I start the server, I get any error. I can see a webpage with the following:
** MongoDB & Mongoose**

When I tried to send my solution link, I get this error:

// running tests
"mongoose version ^5.11.15" dependency should be in package.json
"mongoose" should be connected to a database
// tests completed

I tried the following code:

const { mongoose } = require("mongoose");
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true })

But, this way I can’t connect. This error shows up:

TypeError: mongoose.connect is not a function

This is my package.json file:

{
  "name": "fcc-mongo-mongoose-challenges",
  "version": "0.0.1",
  "description": "A boilerplate project",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "body-parser": "^1.15.2",
    "dotenv": "^8.2.0",
    "express": "^4.18.2",
    "mongodb": "^5.0.1",
    "mongoose": "^5.13.15"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/freeCodeCamp/boilerplate-mongomongoose.git"
  },
  "keywords": [
    "node",
    "mongoose",
    "express"
  ],
  "license": "MIT"
}

And this is an example of my .env file:

MONGO_URI='mongodb+srv://myusername:mypassword@cluster0.n2iu8mg.mongodb.net/Cluster0?retryWrites=true&w=majority'

Idk what else I can do. Anyone, pls can help me?

1 Like

I remove the { } from the const mongoose
Now it’s like this:

const mongoose = require('mongoose');
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });

The error "mongoose" should be connected to a database disappeared.

But I still get this error:

// running tests
"mongoose version ^5.11.15" dependency should be in package.json
// tests completed
1 Like

I run

npm install mongoose@^5.11.15

I manually edited the package.json file from this:

    "mongoose": "^5.13.15"

to this:

    "mongoose": "^5.11.15"
1 Like

For the love of all that is HOLY, thank you Ballar!!! I have been stuck on this for an entire week! I’m pretty sure I understand now what’s happening but have a few really dumb general questions I think you can answer.

  1. I 100% mean this literally because I truly have no clue but how in the world did you figure out that you should run

npm install mongodb
npm install mongoose

I tried downloading the language so many times and to be honest I still don’t 100% know this answer. Anyways I tried downloading the extension on VSCode and just was getting errors like no other so thought I did something really wrong. Then read I should just download the files from the website but that just gave me a CLI software which at least got mongoose code recognized but could never connect to anything! Did you happen to come across an article or some type of resource that explained why you were going to do what they were about to teach you and then like how to execute it.

(Note:After seeing your work it makes sens why you tried to run “npm install mongodb & mongoose” and on top of that Randell suggesting to just run “npm install” makes even more sense. Like, I knew package.json was a file that held all the libraries, languages, etc. needed for anyone to run that specific project locally but I thought that the folder(what ever project we cloned from FFC) came with all that stuff in there already when in fact that I feel that folder is specifically for the code “npm install”!! So once we run that command it’s purpose is to find package.json and download for us everything needed to run the project.) One thing I’m still confused on is if this downloads mongodb/mongoose languages on our systems or only in vscode or is it just in like the node_modules folder; which I believe would mean once I close this folder I would no longer have access to either of those languages/software?

  1. Then 2ndly when making your MongoDB cluster thing the directions here said to Connect to your application but then gave a URI link like mongodb+srv://myusername:mypassword@cluster0.n2iu8mg.mongodb.net/?retryWrites=true&w=majority and said to put the name of the database before ‘?retry’ but have no idea what database they are referring to. The worst part is that the directions immediately skip over it and the 1st sentence of the next set of directions begins with, “If you’ve already created a cluster and a database” lol. Instead of clicking the connect application option did you happen to pick Connect to VSCode and if so was it just as easy as copy and pasting the link in .env or did you happen to do anything more?

Sorry if this is a lot but I just can’t find anything that explains this well!

2 Likes

Hi, @camperextraordinaire!
Yes man, I know that, but when I run

npm install

It installed the latest version, thats why I needed to run

npm install mongoose@^5.11.15

I cloned the boilerplate given at the first lesson of MongoDB and Mongoose topic.

1 Like

Hiii, @Liebmann5
The computer usually says to us what we need to do. For instance, this error at the tests

"mongoose version ^5.11.15" dependency should be in package.json

told me that I havent installed the mongoose. So, I opened the package.json file and there was nothing there about mongodb or mongoose. Then I realize that I should install it.
Usually we use the command line

npm install <and the name of the package/lung/etc>

I installed both Mongoose and MongoDB this way and still didn’t work, this error appeared:

// running tests
"mongoose version ^5.11.15" dependency should be in package.json
// tests completed

So I the problem was the version of mongoose. Thats why I tried to run

npm install mongoose@^5.11.15

This is the command we usually run when we want to install some specific version

npm instal <name_of_the_package>@<the_version_u_wanna_install>
2 Likes

Usually we have to install all the dependencies of our project inside the root directory. For instance, if u start another project, u should install this packages again, if u wanna use them in this specific project.

Every time I clone a repo to do some challenge, I run

npm install

Then I open the package.json file to make sure I have installed all the dependencies I need in order to accomplish the challenge. If I don’t see some specific dependency there, I run

npm instal <name_of_the_dependency>

I had some doubts in this case. So I tried to put the name I gave to this project, and didn’t work.
Try to put the name of the cluster and then worked.
When I started my server running

node server.js

or

npm start

and then access localhost, this page opened:

Than I knew it was working.
Also, no error appeared in my terminal.

I hope my answers helped u.
Sorry I took so long to respond, it was late here :sleeping:
and sorry 'bout my bad english (I’m brazilian :sweat_smile: ), but I tried my best to give understandable answers :rofl: :rofl: :rofl:

I know that, but when I checked my dependencies, the mongoose wasn’t there. I had to install it by myself :woman_shrugging:

1 Like

I may have made a mistake when checking the package.json. I was sure it wasn’t there when I checked, but upon cloning again, I realized it was actually there. Maybe I installed it using the

npm install mongoose

even though it wasn’t necessary. U were right all the time!

1 Like

Oh my gosh that is so incredibly smart man noice! Just to be clear though :rofl: when you said

The computer usually says to us what we need to do.

you meant that when you cloned this boilerplate to vscode the 1st thing you did was run myApp.js and worked your way from there?

Also, I really can’t thank you enough for your detailed notes these were exactly the kind of explanation I needed!! I already printed this page out and taped it to my wall :rofl:!!

1 Like

No, I run

npm install

Then start my server npm start than go to http://localhost:3000/

If I can access the page, usually it means that everything it’s ok (in terms of sintax)
If I can’t access the page or when I start my server some error appears in my console, it means that something wrong isn’t right :rofl:

1 Like

TKS, I really appreciate it. :face_with_hand_over_mouth:
Good to know that I could help u!

1 Like

With the database I’ve noticed you can put anything and nothing and the project will still run and have that page appear ssoooo :melting_face:!

And No your english is incredible! Go you for learning anything and everything! I’m actually mexican and was never taught spanish and have been trying to get myself to start learning but just haven’t :rofl:. Some day though!

2 Likes

Yes, that’s why I meant in terms of syntax. For instance. If I write something wrong, I wont be able to access, but if the syntax it’s ok, I may have to improve the logic of the code or I’m missing some information.
IDN if I could explain the idea correctly in English.

BTW, I grew up with several foreign people, most of them speaking English. After 12 years, this week, literally, I started speaking English again (actually write :sweat_smile:)

2 Likes

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