16

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:pass@mongolab.com: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.

Aidan Collins
  • 215
  • 2
  • 3
  • 8

3 Answers3

11

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!

Community
  • 1
  • 1
alucic
  • 12,492
  • 2
  • 20
  • 25
  • Your first sentence was the hint I needed to hunt through my code to fix the error. I was literally closing the Mongoose connection _before_ a callback that did bulk insertion got the chance to complete its task. Thanks! – Saïd May 15 '16 at 22:56
  • 1
    Stupidly, I was getting this "topology" error because I was immediately closing my db connection from copy and pasting code, thus I couldn't do any other DB operations. At least your link led me to inspecting my code closer. – HaulinOats Feb 22 '19 at 20:20
9

Having experienced the same problem, I found that Mongolab recommends to apply the following settings in order to keep Mongodb's connection alive in production:

var options = {
  server: { socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } },
  replset: { socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } }
};
mongoose.connect(secrets.db, options);

I hope that this will help you, or other people having this "Topology was destroyed" problem.

wizzardz
  • 5,664
  • 5
  • 44
  • 66
Adrien Joly
  • 5,056
  • 4
  • 28
  • 43
  • What will happen after connectTimeoutMS: 30000, it will not try again? by default it's 30 seconds right? – 151291 Jan 30 '20 at 09:30
1

I had the same problem. Then I had the idea to upgrade my mongoose library . But as I was running npm install mongoose appeared the error " ... kerberos errors ( gssapi / gssapi.h file not found) ... " . So after some research I saw that to solve enough to run apt- get install libkrb5 -dev or to Had Hat yum install krb5 -devel . After I did npm install the mongoose and solved my problem

gilmar
  • 11
  • 1
  • Can you clean up your answer and clarify some of the steps? It may be right, I just don't understand it. – blm Nov 13 '15 at 21:20
  • I just followed this advice and it worked for me. `sudo aptitude install libkrb5-dev` && `rm -rf node_modules` && `npm install` And that is it. Thanks @gilmar – Mestre San Nov 20 '15 at 04:05