2016-12-28 12 views
1

ブラウザから解析したデータをmongodbデータベースに保存しようとすると問題が発生します。それを修正する方法、ありがとう。nodejsを使用してmongodbデータベースの問題にデータを保存して表現する

App.js:

var print = require('./print'); 
var port = 1338; 
var express = require("express"); 
var app = express(); 
var bodyParser = require('body-parser'); 
var mongodb = require('mongodb'); 
var MongoClient = mongodb.MongoClient; 
var url = 'mongodb://localhost:1337/testdb'; 
MongoClient.connect(url, function(err, db) { 
    var collection = db.collection('users'); 
    if (err) { 
     console.log('Unable to connect to the mongoDB server. Error:', err); 
    } else { 
     console.log('Connection established to', url); 
    } 
    app.use(bodyParser.urlencoded({ extended: true })); 
    app.set('view engine', 'ejs'); 
    app.get('/', function(req, res) { 
     res.render("post"); 
     print("All is in order"); 
    }) 
    var collection = db.collection('users'); 
    var name = app.post('/post', function(req, res) { 
     res.send('You sent the name "' + req.body.name + '".'); 
     var name = req.body.name 
     var user1 = { "username": name }; 
     collection.insert(user1, function(err, result) { 
      if (err) { 
       console.log(err); 
      } else { 
       console.log('Inserted %d documents into the "users" collection. The documents inserted with "_id" are:', result.length, result); 
      } 
     }) 
    }); 
    app.listen(port, function() { 
     print(`listening on ${port}`); 
    }) 
    db.close(); 
}) 

Post.ejs:

<!DOCTYPE html> 
<html> 

<head> 
    <meta charset="utf-8" /> 
    <title>CSS3 Contact Form</title> 
</head> 

<body> 
    <div id="contact"> 
     <h1>Send an email</h1> 
     <form action="http://127.0.0.1:1338/post" method="post"> 
      <fieldset> 
       <label for="name">Name:</label> 
       <input type="text" id="name" name="name" placeholder="Enter your full name" /> 
       <label for="email">Email:</label> 
       <input type="email" id="email" placeholder="Enter your email address" /> 
       <label for="message">Message:</label> 
       <textarea id="message" placeholder="What's on your mind?"></textarea> 
       <input type="submit" value="Send message" /> 
      </fieldset> 
     </form> 
    </div> 
</body> 

</html> 

エラー:

{ MongoError: server instance pool was destroyed 
     at Function.MongoError.create (C:\Users\Programming\Documents\Web_Repo\node_modules\mongodb-core\lib\error.js:29:11) 
     at basicWriteValidations (C:\Users\Programming\Documents\Web_Repo\node_modules\mongodb-core\lib\topologies\server.js:446:51) 
     at Server.insert (C:\Users\Programming\Documents\Web_Repo\node_modules\mongodb-core\lib\topologies\server.js:532:16) 
     at Server.insert (C:\Users\Programming\Documents\Web_Repo\\node_modules\mongodb\lib\server.js:383:17) 
     at executeCommands (C:\Users\Programming\Documents\Web_Repo\node_modules\mongodb\lib\bulk\ordered.js:455:23) 
     at OrderedBulkOperation.execute (C:\Users\Programming\Documents\Web_Repo\node_modules\mongodb\lib\bulk\ordered.js:508:10) 
     at bulkWrite (C:\Users\Programming\Documents\Web_Repo\node_modules\mongodb\lib\collection.js:652:8) 
     at Collection.insertMany (C:\Users\Programming\Documents\Web_Repo\node_modules\mongodb\lib\collection.js:522:44) 
     at Collection.insert (C:\Users\Programming\Documents\Web_Repo\node_modules\mongodb\lib\collection.js:824:15) 
     at C:\Users\Programming\Documents\Web_Repo\app.js:36:18 
     name: 'MongoError', 
     message: 'server instance pool was destroyed' } 
+1

をconfig.jsの) '。あなたが書いたコードは非同期で実行されるので、何かをフェッチする前に接続を閉じます。 –

+1

コードの書き方は理想的ではありませんが、問題をすばやく解決するには、 'collection.insert'のコールバックに' db.close() 'を入れてください。 –

+0

どうもありがとうございます。コード構造は? –

答えて

0

ムケシュはすでにあなたに解決策を告げたようですが、彼はあなたのコードの構造を言及したように見えます改善することができる。

だからここに、私はそれを行うだろうかだ

設定ファイル〜は、それが原因(db.close `である

module.exports = { 
    localUrl: 'mongodb://localhost/testDb', 
    port: 1338 
}; 

メインファイル

var print = require('./print'); 
var express = require("express"); 
var app = express(); 

var bodyParser = require('body-parser'); 
var mongodb = require('mongodb'); 
var MongoClient = mongodb.MongoClient; 
var config = require('./config.js'); 

app.use(bodyParser.urlencoded({ extended: true })); 
app.set('view engine', 'ejs'); 
app.get('/', function(req, res) { 
    res.render("post"); 
    print("All is in order"); 
}) 

app.listen(config.port, function() { 
    print(`listening on ${config.port}`); 
}) 

MongoClient.connect(config.localUrl, function(err, db) { 
    if(err) { 
     console.log('Unable to connect to the mongoDB server. Error:', err); 
     return; 
    } 
    var collection = db.collection('users'); 
    app.post('/post', function(req, res) { 
     res.send('You sent the name "' + req.body.name + '".'); 
     collection.insert({ 
     username : req.body.name 
     }, function(err, result) { 
      if (err) { 
       console.log(err); 
       db.close(); 
       return; 
      } 
      db.close(); 
     }) 
    }); 

}) 
+0

マイケルありがとう! –

+0

Npカイル...私は助けてくれてうれしい...私の答えがあなたが探していたものであれば、それはupvoteすることができます –

+0

あなたはコメントとして投稿しましたupvoteできませんでしたか?もしそうなら、あなたは答えとしてそれを再投稿することができ、あなたに十分に値する担当者を与えるためにそれをアップボケします。 –

関連する問題