The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

Cathair96 (1) [Avatar] Offline
#1
Hi, I realise the first edition of Getting MEAN may contain some redundant code as languages evolve & a second edition is coming but I have found this book to be very beneficial so far until now where I have hit a brick wall.

In 5.1.2 under Creating the Mongoose Connection, it tells us to add the following to db.js

var dbURI = 'mongodb://localhost/Loc8r';
mongoose.connect(dbURI);


And in the next section: Monitoring the connection with Mongoose connection events, it shows us how to print connection statuses to the console. I have done both of these sections correctly but I am still getting an error when refreshing the localhost:3000. The error is displayed in the terminal and contrary to the console-printing errors that were coded, the errors/connection statuses do not make their way to the console. Nothing happens on the console at all. Just the terminal to tell me:

Mongoose connection error: mongodb://127.0.0.1/Loc8r
(node:743) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoNetworkError: failed to connect to server [127.0.0.1:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
(node:743) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Help would be gladly appreciated.

Thanks.
644652 (3) [Avatar] Offline
#2
When you say you saw the error "in the terminal" if you're seeing it in the terminal with which you're running your app, and if that's the only terminal you're using, that's your problem I think. You need to run two terminals - one for the app, and another for the database. It wasn't 100% clear to me as I read the book how all this worked, but that's what I discovered.

So two things you might check:
- have you started MongoDB?
- is the folder MongoDB is using already created?

What I found was that I needed to start MongoDB using a separate Git Bash terminal from the one I then use to run the app, and that in that second terminal for MongoDB I needed to do a command like the following to run it...

$ "C:\Program Files\MongoDB\Server\4.0\bin\mongod.exe" --dbpath="c:\data\db"

This runs MongoDB (i.e. it points to where you installed mongod.exe to run it), and tells MongoDB to save database data to files in C:\data\db. Note: "db" is not the name of your database, it's the folder used. I manually created that filepath; it could be anywhere you desired.

If that wasn't your problem, did you require the db file in your main app.js file? It might help me to investigate further if you show the entire contents of that app.js file and the db.js file.