gpt4 book ai didi

javascript - 蒙哥错误: topology was destroyed(when finding the documents) and instance pool was destroyed (when inserting documents)

转载 作者:行者123 更新时间:2023-11-29 16:46:14 27 4
gpt4 key购买 nike

我正在尝试使用 Node.js、Express 和 Mongodb 构建 REST API。我正在使用 mongodb npm 包连接到数据库,下面是我的 sever.js 文件代码

var express = require("express");
var app = express();
var bodyParser = require("body-parser");
var port = process.env.PORT || 8080;
var mongo = require("mongodb");
var Server = mongo.Server;
var Db = mongo.Db;
var ObjectID = mongo.ObjectID;
try{
var config = require('./configure.js');
}catch(e){
console.log("configuration file is hidden on github for security");
config = null;
}
var usersCollection = config.usersCollection;
var login = require('./routes/login/index.js');
var signup = require('./routes/signup/index.js');
var Database = new Db(process.env.DBNAME || config.DBNAME , new Server(process.env.DBHOST || config.DBHOST, process.env.DBPORT || config.DBPORT, {'native_parser': true}));


Database.open(function (err, mongoclient) {
if(err){
console.log("Failed to connect to the database. Please Check the connection");
throw err;
}else{
Database.authenticate(process.env.DBUSER || config.DBUSER, process.env.DBPASS || config.DBPASS, function(err, res) {
if (err) {
console.log("Authentication Failed");
throw err;
}else{
console.log("Connected to the database Successfully");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var users = Database.collection(usersCollection);
login(app,users);
signup(app,users);

Database.close();
}
});
}
});

app.listen(port,function(){
console.log("Server Started Listening to port : "+port);
});

路由/登录/index.js

module.exports = function(app,users){
app.route('/login')
.post(function(req,res){
var username = req.body.username;
var password = req.body.password;
var query = {'FirstName':username};

users.find().toArray(function(err,docs){
if(err){
throw err;
}else{
console.log("no err");
if(doc.LastName == password){
res.send({status : true});
}else{
res.send({status : false});
}
}
});
});

和 routes/signup/index.js

module.exports = function(app,users){
app.route('/signup')
.post(function(req,res){
var doc = { EmpNo:"1",
FirstName:"Andrew",
LastName:"Neil",
Age:"30",
Gender:"Male",
Skill:"MongoDB",
Phone:"408-1234567",
Email:"Andrew.Neil@gmail.com",
Salary:"80000"
};

users.insert(doc,function(err,info){
if(err){
throw err;
}else{
console.log('Successfully inserted ' + JSON.stringify(info));
res.send({result: 'done'});
}
});
});

当我尝试在 server.js 中插入文档时,我能够成功插入它们,但是通过 routes/signup/index.js 我正在获取实例池,如果我尝试在server.js 没有错误,但如果我尝试从 routes/login/index.js 执行此操作,那么我会收到错误消息,因为拓扑结构已被破坏。

需要帮助来解决它。

最佳答案

如果连接在进程中间以某种方式被切断,则会抛出这些错误。

因为 users.find()users.insert() 函数都是异步的,您的 server.js 文件到达 Database.close() 函数并在完成该过程之前关闭与数据库的连接,从而给出错误。

关于javascript - 蒙哥错误: topology was destroyed(when finding the documents) and instance pool was destroyed (when inserting documents),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41170727/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com