-問題描述最近在學(xué)習(xí)使用express和mongodb;做了demo例子,發(fā)現(xiàn)個問題,就是每次從頁面上發(fā)送請求往表里添加數(shù)據(jù)時,第一次正常,第二次就報錯了?-對應(yīng)代碼express版本4.16.4mongodb版本3.1.10//expressconstexpress=require('express')constbodyParser=require('body-parser')constapp=express()//mongodbconsturlDB='mongodb://localhost:27017'constMongoClient=require('mongodb').MongoClientconstClient=newMongoClient(urlDB,{useNewUrlParser:true})//mongodbfunctionconstinsertDoc=function(db,collectionName,data,callback){constcollection=db.collection(collectionName)collection.insertOne(data,(err,res)=>{callback&&callback(err,res)})}app.use(bodyParser.urlencoded({extended:true}))//使用虛擬目錄的靜態(tài)服務(wù)app.use('/static',express.static('public'))//路由app.get('/',(req,res)=>{res.sendFile(__dirname+'/index.html')})//應(yīng)用app.post('/insert',(req,res)=>{//mongodbClient.connect((err)=>{constdb_=Client.db('user')console.log('-連接成功-',err);insertDoc(db_,'user',req.body,(errDB,resDB)=>{if(errDB){console.log('-插入失敗-',errDB);res.send({succ:false})}else{resDB.result.ok&&res.send({succ:true})Client.close()}})})})//端口號app.listen(3000)-錯誤代碼theoptions[servers]isnotsupportedtheoptions[caseTranslate]isnotsupported-連接成功-null-插入失敗-{MongoError:serverinstancepoolwasdestroyedatbasicWriteValidations(/Users/chisecj/Documents/project/study/test_node/09_express/node_modules/_mongodb-core@3.1.9@mongodb-core/lib/topologies/server.js:700:41)atServer.insert(/Users/chisecj/Documents/project/study/test_node/09_express/node_modules/_mongodb-core@3.1.9@mongodb-core/lib/topologies/server.js:805:16)atServer.insert(/Users/chisecj/Documents/project/study/test_node/09_express/node_modules/_mongodb@3.1.10@mongodb/lib/topologies/topology_base.js:321:25)atinsertDocuments(/Users/chisecj/Documents/project/study/test_node/09_express/node_modules/_mongodb@3.1.10@mongodb/lib/operations/collection_ops.js:838:19)atinsertOne(/Users/chisecj/Documents/project/study/test_node/09_express/node_modules/_mongodb@3.1.10@mongodb/lib/operations/collection_ops.js:868:3)atexecuteOperation(/Users/chisecj/Documents/project/study/test_node/09_express/node_modules/_mongodb@3.1.10@mongodb/lib/utils.js:420:24)atCollection.insertOne(/Users/chisecj/Documents/project/study/test_node/09_express/node_modules/_mongodb@3.1.10@mongodb/lib/collection.js:464:10)atinsertDoc(/Users/chisecj/Documents/project/study/test_node/09_express/app.js:12:14)atClient.connect(/Users/chisecj/Documents/project/study/test_node/09_express/app.js:30:5)atresult(/Users/chisecj/Documents/project/study/test_node/09_express/node_modules/_mongodb@3.1.10@mongodb/lib/utils.js:414:17)name:'MongoError',[Symbol(mongoErrorContextSymbol)]:{}}
關(guān)于 express 中使用 mongodb 第二次失敗問題十分感謝
qq_笑_17
2019-08-21 19:37:06