3

I'm new to mongoose and javascript in general, and want to connect to a locally running mongodb. The following code give no errors, will perform all of the console logs except for the one inside the db.on('connected', function() call. The console log before that prints out a 1, and I can see on my mongodb terminal that a connection is being accepted. What am I missing here?

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/rawStockData');

https.get(requestURL, function(res) {
  var data = '';
  res.on('data', function (chunk) {
    data += chunk
  });
  res.on('end', function() {
    var jsonObj = JSON.parse(data);
    console.log('Data Parsed');

    var db = mongoose.connection;
    db.on('error', console.error.bind(console, 'connection error:'));
    console.log(db.readyState);
    db.on('connected', function() {
      console.log('ConnectedToDatabase!');
    });
  });
});
3
  • 1
    What are you trying to achieve here? Mongoose will queue any query until it's connected so you shouldn't need to do any of this in your request handler. Commented Feb 22, 2018 at 13:59
  • I'm trying to get historic stock data through the API and put it in the mongodb. My thinking was, due to the asynchronous nature of javascript, I need to make sure I'm connected before I tried to save the data. Is this unnecessary? Commented Feb 22, 2018 at 14:12
  • 1
    Yeah, it's unnecessary with Mongoose as it already waits until the connection is established before saving. Commented Feb 22, 2018 at 14:20

2 Answers 2

2

In your case mongoose.connect is called before the event listeners are installed.

You attach the event listener for open at a much later point in time (when a request is being handled).

Try to edit your code and put the event listeners just after the connect function.

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/rawStockData');


var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.on('connected', function() {
   console.log('ConnectedToDatabase!');
});

https.get(requestURL, function(res) {
  var data = '';
  res.on('data', function (chunk) {
    data += chunk
  });
  res.on('end', function() {
    var jsonObj = JSON.parse(data);
    console.log('Data Parsed');
  });
});
Sign up to request clarification or add additional context in comments.

2 Comments

This fixed it! Thank you!
I did, but I have less than 15 reputation so it isn't shown publicly
0

I think you just have to replace your

db.on('connected', ..)

to

db.once('connected', ..)

You can also have something like this :

mongoose.connection.once('connected', function(err) {
    if (err) {
        console.log(err);
    }
    else {
        console.log("Connected to Database");
    }
}).on('error', function (err) {
    console.log(err);
}).on('disconnected', function () {
    console.log('Disconnected to database');
});

Hope it helps.

1 Comment

This did not solve the problem. Nothing about the behavior in the original post has changed, but thank for you trying to help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.