Mongolab nodejs topology destroyed

Aidan Collins picture Aidan Collins · May 24, 2015 · Viewed 13.7k times · Source

I have been interfacing with twitter using nodejs. I'm trying to log some important public user data in a mongolab mongodb database. For some reason I keep getting a "topology destroyed error" I'm not quite sure why this is.

var Twitter = require('twitter');
var mongodb = require('mongodb');

var accounts = ['@zaynmalik',
'@ZooeyDeschanel'];

var client = new Twitter({
  consumer_key: 'key',
  consumer_secret: 'secret',
  access_token_key: 'key',
  access_token_secret: 'secret'
});

var MongoClient = mongodb.MongoClient;
var url = "mongodb://user:[email protected]:numbers/db";

MongoClient.connect(url, function (err, db) {
  if (err) {
    console.log('Unable to connect to the mongoDB server. Error:', err);
  } else {
    //HURRAY!! We are connected. :)
    console.log('Connection established to database');

    var collection = db.collection('accounts');

    for(var i = 0; i < accounts.length; i++){
        client.get('users/show', {screen_name: accounts[i]}, function(error, tweets, response){
          if(error) console.log(error);
              var account = {'screen_name': accounts[i], 'id': tweets.id};
              collection.insert(account, {w:1}, function(err, result) {console.log(err);});
              //collection.insert(account);
              console.log(tweets.id);  // Raw response object. 
        });

}

    db.close();
  }
});

As you can see the program establishes a connection to the database. Defines the collection and then iterates through a number of twitter accounts and logs pertinent information. The twitter requests are successful and the mongodb works with simple requests. If you have any ideas about why I'm getting this response please answer.

Answer

alucic picture alucic · Jul 11, 2015

I had similar problem, your database connection gets closed before all of your requests to twitter are done and data inserted.

I ended up sending callback to my function like they do it in documentation.

https://github.com/mongodb/node-mongodb-native#inserting-a-document

You can see after insertion is done they call callback(result);

And that is just anonymous function that calls db.close()

Here are some other links that might help you out with opening/closing db connections

When to close MongoDB database connection in Nodejs

Why is it recommended not to close a MongoDB connection anywhere in Node.js code?

Keeping open a MongoDB database connection

Hope it helps!