Mongoose (MongoDB) Connection issue

Hi, i’m following this tutorial to build my first API:

I believe it was some miscommunication by the part of the author, when in this line of code he says we need to connect to this database:

// Require external modules
const mongoose = require('mongoose')

// Connect to DB
mongoose.connect(‘mongodb://localhost/mycargarage’)
 .then(() => console.log(‘MongoDB connected…’))
 .catch(err => console.log(err))
 

Well, first of all, i don’t have this “mycargarage” db, so how do i set it?

Second, i’m having some connection issues with mongo, when i type mongo on my shell i get an error, which i believe is not related to the tutorial, just some issue with my installation, it says:

Error: couldn’t connect to server 127.0.0.1:27017, connection attempt failed: SocketException:

Already searched through MongoDB documentation it says nothing about the 1st issue nor the 2nd one, even searching through StackOverflow i could figure it out.

How can i solve the first issue? I mean i think i can find if i search more, just saying in case anyone here know an easy fix, no need to bother, the 1st is my biggest concern right now.

In advance, thank you very much!

This looks like the correct setup if you were running mongoDB locally, and the mongodb://localhost/mycargarage would be a valid connection URI to the collection mycargarage on the local mongoDB server. So, if you’ve got node and npm installed locally, you can also install and run mongoDB locally as well, but you should consult their documentation for your operating system. This is a much faster development setup, but you have to know how to install everything properly yourself. It’s not hard, but there is a learning curve.

If you don’t want this stuff locally, sign up for an account at repl.it (or similar) and work on your project there and get a mongoDB.com account and set up your DB there. They will provide you with the connection URI to use for mongoose.connect(). Both these accounts are free. This setup is slower, but all the administrative stuff is done for you, correctly, and it just works as long as you don’t mind having to work online.

2 Likes