0

I am trying to follow this tutorial: https://medium.com/@bryantheastronaut/react-getting-started-the-mern-stack-tutorial-feat-es6-de1a2886be50

I'm stuck because I can't find a way to connect my mLab database to node. I have figured out that the <> around the user name and password need to be removed from the string, but I still can't get it to connect.

mongoose.connect('mongodb://Testerxxx:Testxxx@ds157057.mlab.com:57057/testxxxx');

The log shows the error as:

(node:91198) DeprecationWarning: `open()` is deprecated in mongoose >= 4.11.0, use `openUri()` instead, or set the `useMongoClient` option if using `connect()` or `createConnection()`. See http://mongoosejs.com/docs/connections.html#use-mongo-client
09:37:34 api.1   |  events.js:183
09:37:34 api.1   |        throw er; // Unhandled 'error' event
09:37:34 api.1   |        ^
09:37:34 api.1   |  Error: listen EADDRINUSE :::3001
09:37:34 api.1   |      at Object._errnoException (util.js:1022:11)
09:37:34 api.1   |      at _exceptionWithHostPort (util.js:1044:20)
09:37:34 api.1   |      at Server.setupListenHandle [as _listen2] (net.js:1351:14)
09:37:34 api.1   |      at listenInCluster (net.js:1392:12)
09:37:34 api.1   |      at Server.listen (net.js:1476:7)

When I follow the link to the readme for this error: http://mongoosejs.com/docs/connections.html#use-mongo-client, I can't actually see any instructions that differ from those in the tutorial.

Can anyone see what's wrong with this from. I've used the exact string from the Mongo DB connect page, inserting the mongoose.connect bit at the front.

Can anyone point to more detailed instructions for connecting to mLab?

Another hypothesis:

When I run npm run start-dev, the terminal shows a message that says:

Local: http://localhost:3000/ 12:58:52 web.1 | On Your Network: http://10.0.0.2:3000/ 12:58:52 web.1 |

However, the page I'm trying to check is port 3001 (per the tutorial). My server.js file also asks for the 3001 port to be used:

var port = process.env.API_PORT || 3001;

Maybe the reason why I can't get a response for the mLab connection is something to do with the difference in ports. If that's the case, I don't understand because, to this point, I have followed the tutorial in the setup.

Mel
  • 2,481
  • 26
  • 113
  • 273
  • Hi Mel, what error are you seeing when you run your code? Can you edit your question to include the error, please? – tfogo Jan 18 '18 at 00:07
  • I also just noticed that your hostname `ds0157057.mlab.com` looks incorrect. The first part of the hostname should be `ds` followed by 6 digits. Yours has 7 digits. Can you check that you have copied it correctly? Perhaps it is meant to be `ds157057.mlab.com`? – tfogo Jan 18 '18 at 00:20
  • Hi @tfogo - that was actually a typo by me in formatting this post. Thanks - I removed the 0 from the db string in this question - but it isn't part of my connection string. – Mel Jan 18 '18 at 00:37
  • The `EADDRINUSE` error is an error with express, not mongoose. It means you already have a server running on port 3001. Make sure you close the node process before running it a second time. Once you've fixed the `EADDRINUSE` error, let me know if there's another error. – tfogo Jan 18 '18 at 01:13
  • By the way, the mongoose deprecation warning is nothing to worry about. You can get rid of it by changing your code to this: `mongoose.connect('mongodb://Testerxxx:Testxxx@ds157057.mlab.com:57057/testxxxx', {useMongoClient: true})`. This isn't necessary in the newest version of mongoose (version 5.0). That's why it isn't in the docs you looked at. – tfogo Jan 18 '18 at 01:19
  • Thanks @tfogo - I can't get any response to the mLab connection. I can follow the tutorial and render localhost:3001/api (and get the same result as the tutorial, but I can't get the same result when I append /comments, as per the tutorial. Maybe something in the settings configuration has changed since the tutorial was published. – Mel Jan 18 '18 at 03:03
  • Hi again, are you able to run the server without getting the `EADDRINUSE` error? This error would suggest you have started the server (that is, run `npm start` or `npm run start-dev`) then not closed the server before trying to run it again. So you still have an express server running, but it's running old code. This is the first problem to address, then we can see if there are any issues connecting to your mLab database. – tfogo Jan 18 '18 at 03:20
  • Here are instructions on how to make sure your node server is shutdown before running it again: https://stackoverflow.com/questions/14790910/stop-all-instances-of-node-js-server/14790921#14790921 – tfogo Jan 18 '18 at 03:23
  • Thanks! I had another terminal window open in the background. Thanks very much. I can move on to the next step now. :) – Mel Jan 18 '18 at 03:25
  • 1
    Great to hear it's working! FYI, if you encounter any issues with your mLab database, you can also email support@mlab.com. We're always happy to help. Good luck with the tutorial! – tfogo Jan 18 '18 at 03:28

0 Answers0