Mongolab nodejs topology destroyed

白昼怎懂夜的黑 提交于 2019-12-03 02:57:10
alucic

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!

Adrien Joly

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!