我想在完成操作后就关闭mongodb客户端,因为我的连接限制一直达到100。但是每当我关闭连接时,都会出现此错误:
MongoError: Topology was destroyed
我的代码:
test.js-将要执行的JS文件
const db = require('./dbNew')
db.checkUserID(124214).then(function(result){
if(result == false){
db.getAccessToken(810191770).then(function(result2){
console.log(result)
})
}
});
dbNew.js-我的数据库模块文件
module.exports = {
checkUserID: function(userID){
var isExist;
return new Promise(function(resolve,reject){
client.connect(err => {
const collection = client.db("over_ride").collection("users");
collection.find({user_id:userID}).count().then(function(checkCount){
if(checkCount>0){
isExist = true;
}
else{
isExist = false;
}
resolve(isExist);
client.close()
})
});
});
},
getAccessToken: function(userID){
return new Promise(function(resolve,reject){
client.connect(err => {
const collection = client.db("over_ride").collection("users");
collection.findOne({user_id:userID}).then(function(result){
console.log(result)
})
client.close()
// resolve(result.social_media.viber.access_token);
})
})
}
}
答案 0 :(得分:0)
完成findOne
操作后,您应该关闭连接:
collection.findOne({user_id:userID}).then(function(result){
console.log(result)
client.close()
})