1

I have been following online instructions to set up a mongodb database using mongoose on node.js. I have managed to get the mongodb running and listening on port 27017, however when I run my connection code in node.js I get a successful response even when the mongo.db isn't running and I don't see any updates on the mongo.db to say it has received any data.

I have tried quite a few different examples from the internet but can't get any of them to work.

So I have my MongoDB saying:

2019-03-26T12:00:46.039+0000 I NETWORK  [initandlisten] waiting for connections on port 27017

This is the basic code I am trying to get to work as my original API wasn't working:

//sample.js

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/test', function(err){

  if(!err) console.log('Successfully Connected');

  console.log(mongoose.connection.host);

  console.log(mongoose.connection.port);
});

I receive this response when I run

node sample.js

Successfully Connected

localhost

27017

But I receive this response even when my mongo.db has been shut down, so I think there is some error, I'm not sure how to check if they are connecting properly.

laruiss
  • 3,780
  • 1
  • 18
  • 29
Chelsea
  • 11
  • 3

2 Answers2

2

this works for me. try to set some debugging to find out what is happening.

mongoose.set('debug', true);

mongoose.connect("mongodb://localhost:27017/test", {useNewUrlParser: true})
    .then(() => {
        console.log('connected to the db');
     })
    .catch(err => {
        console.log('connection failed ' + err);
    }); 
jcuypers
  • 1,774
  • 2
  • 14
  • 23
  • Hi I tried to do this but I got some errors - unexpected token at : '.catch' – Chelsea Mar 27 '19 at 12:06
  • sorry, I missed a couple of brackets when copy-ing. please try again – jcuypers Mar 27 '19 at 12:34
  • Thanks, it works but still says 'connected to the db' when I haven't started my mongodb. Could it maybe be running without me knowing? – Chelsea Mar 27 '19 at 13:08
  • It might be. Which OS you use? try `mongo` to connect to it via command-line. it will show the status of the service if running – jcuypers Mar 27 '19 at 13:14
  • When I type mongo it does seem to connect: connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb My database is currently empty but I can find it on there. I was under the impression I had to have a separate mongo shell running to use this but maybe not? – Chelsea Mar 27 '19 at 13:29
  • So there is already a db running :) other ways to validate would be to use a `telnet localhost 27017` to check if the port is opening and / or to use a tool to manage mongodb. Studio 3T has a trial. its very nice to manage mongodb instances (just verify your setup with it). so I guess your problem is solved now? Another way to test: just stop the process if like this post: https://stackoverflow.com/questions/11774887/how-to-stop-mongo-db-in-one-command/11777141. and see if you get a failure to connect – jcuypers Mar 27 '19 at 13:35
  • 1
    Thank you! I think I had the wrong understanding of how it works. – Chelsea Mar 27 '19 at 13:51
1

I think you have to check the API to connect.

https://mongoosejs.com/docs/connections.html

Second parameter is option and third is callback - but u seem to have passed callback as second param - also, check what are you getting as response

mongoose.connect(uri, options, function(error) {
  // Check error in initial connection. There is no 2nd param to the callback.
});

// Or using promises
mongoose.connect(uri, options).then(
  () => { /** ready to use. The `mongoose.connect()` promise resolves to undefined. */ },
  err => { /** handle initial connection error */ }
);
takrishna
  • 4,884
  • 3
  • 18
  • 35