Home » Nodejs » Mongolab nodejs topology destroyed

Mongolab nodejs topology destroyed

Posted by: admin November 29, 2017 Leave a comment

Questions:

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.

Answers:

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!

Questions:
Answers:

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.

Questions:
Answers:

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