Mongo抛出“拓扑已被破坏"一次连接后 [英] Mongo throwing "Topology was destroyed" after one connection
本文介绍了Mongo抛出“拓扑已被破坏"一次连接后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的是由mlab和Node.js托管的MongoDB,以及express和TypeScript.我的问题是,当我运行服务器时,我只能向数据库发出一个请求,而随后的任何请求都抛出拓扑已被破坏." .这就是我的代码.
I am using MongoDB hosted with mLab and Node.js with express and TypeScript. My problem is that I can make exactly one request to the database when I run my server, and any subsequent request throws "Topology was destroyed." Here's what my code looks like.
export function getTeamName(id: string, callbackSuccess: (name) => void, callbackError?: (error) => void) {
initDb(() => {
db.collection('teams', (err: Error, teams) => {
if (err) { callbackError(err); db.close(); return; }
else {
teams.findOne({ '_id': id }, { 'name': 1 }, (error, name) => {
if (error) { callbackError(error); db.close(); return; }
else { callbackSuccess(name); db.close(); }
});
}
})
}, (err) => {
callbackError(err);
})
}
和 initDb()
方法:
import { Server, Db } from 'mongodb'; //using mongodb typings
var server = new Server("*******.mlab.com", *****, { auto_reconnect: false });
var db = new Db('serverName', server, { w: 1 });
function initDb(callbackSuccess: (data) => void, callbackError?: (err) => void) {
db.open((err, db) => {
if (err) {
callbackError(err);
}
else {
db.authenticate("username", "password", (error, data) => {
if (error) {
callbackError(err);
}
else {
callbackSuccess(data);
}
});
}
});
}
谢谢您的帮助.
推荐答案
确保您的网络没有防火墙...这是我的问题.
Make sure your network does not have a firewall... That was my issue.
这篇关于Mongo抛出“拓扑已被破坏"一次连接后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文