前提・実現したいこと
Expressを利用してユーザからのPOSTを受け付け、MongoDBに保存する仕組みを作っています。
発生している問題
MongoDBのNODE.JS DRIVERのドキュメントを参考に、下記ソースコードを作成しました。
実際に動かしてみたところ、最初うまくPOSTでき、保存成功するものの、
もう一度POSTを行うと、
the options [servers] is not supported the options [caseTranslate] is not supported the options [dbName] is not supported the options [credentials] is not supported
と出力。続いて「connected correctly.」と出力された後に、
Error: server instance pool was destroyed
とのAssertionErrorが発生してアプリが終了してしまいます。
適切な修正方法がわからず困っています。
ご教授いただければ幸いです。
該当のソースコード
JavaScript
1const MongoClient = require('mongodb').MongoClient; 2const assert = require('assert'); 3const user = encodeURIComponent('ユーザー名'); 4const pwd = encodeURIComponent('パスワード'); 5const authMechanism = 'DEFAULT'; 6 7const dbName = 'データベース名'; 8const url = `mongodb://${user}:${pwd}@127.0.0.1:27017/${dbName}?authMechanism=${authMechanism}`; 9 10const client = new MongoClient(url, { useNewUrlParser: true }); 11 12var app = express(); 13var server = app.listen(3000, function(){ 14 console.log("Node.js is listening to PORT:" + server.address().port); 15}); 16app.use(bodyParser.json()); 17app.use(bodyParser.urlencoded({ extended: true })); 18 19app.post("/", function(req, res, next){ 20 var text = req.body.text; 21 client.connect(function(err, client){ 22 assert.equal(null,err); 23 console.log("connected correctly.") 24 const col = client.db(dbName).collection('test'); 25 col.insertOne({"text":text},function(err,doc){ 26 assert.equal(null, err); 27 client.close(function(){ 28 console.log("closed correctly."); 29 res.send("saved successfully."); 30 }); 31 }); 32 }); 33});
試したこと
インスタンス化した1つのMongoClientを接続したり切断したりすることはできないのでは?と考え、いくつかの修正案を試行しました。
正常に動作する方法をいくつか見つけましたが、やりたいこと(Webアプリケーションサーバ)に対して適切な方法であるのか不安があります(POSTに応じて同じデータベースに何度も保存を行う場合、適切にインスタンス化してそれを保持することで接続を速められる?)。
[修正案1]
インスタンス化の場所をapp.post内に移したところ、1度目のPOSTだけでなく2度目以降も正常に動作しました。この方法だと、POSTの度にインスタンスを作成するように思い、適切なのか不安があります。
//修正案1 app.post("/", function(req, res, next){ var text = req.body.text; const client = new MongoClient(url, { useNewUrlParser: true }); client.connect(function(err, client){ assert.equal(null,err); console.log("connected correctly.") const col = client.db(dbName).collection('test'); col.insertOne({"text":text},function(err,doc){ assert.equal(null, err); client.close(function(){ console.log("closed correctly."); res.send("saved successfully."); }); }); }); });
[修正案2]
次に、いくつかのサイトで見かけたインスタンスを作成しない方法を試しました。これも正常に動作しました。特に非効率がないのであれば、この方法でも構わないのでしょうか。
//修正案2 //const client = new MongoClient(url, { useNewUrlParser: true });は記述しない。 app.post("/", function(req, res, next){ text = req.body.text; MongoClient.connect(url, function(err, client){ assert.equal(null,err); console.log("connected correctly.") const col = client.db(dbName).collection('test'); col.insertOne({"text":text},function(err,doc){ assert.equal(null, err); client.close(function(){ console.log("closed correctly."); res.send("saved successfully."); }); }); }); });
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/16 05:50