Mongodb:不支持选项[...]

ree

在这里,我有一个服务器和一个进行ajax调用的html文件。发布时,我想将用户名和密码放入数据库中。当我首先加载客户端并发送信息时,它成功进入数据库,但是如果我发送另一批信息,则会收到错误消息:以下是错误消息:

成功连接到服务器将3个文档插入到集合中不支持选项[服务器]选项不支持选项[caseTranslate]不支持选项[dbName]成功连接到服务器将3个文档成功连接到服务器将3个文档插入集合

在重新启动服务器之前,我的数据库将不再填充。有人能指出我正确的方向吗?这是我在.on POST事件内部构造客户端连接的方式吗?

的HTML

<!DOCTYPE html>
<html>
    <head>
        <title>
            Little structure
        </title>
    </head>
    <body>

        <div id="demo">
            <h1>Click ere</h1>
            <button type="button" onclick="loadDoc()">Submit</button></br>
            <input id = "userName"></input> </br>
            <input id = "userPW" type = "password"></input></br>
        </div>

        <script>



            function loadDoc() {
                    //get user value
                    var userName = document.getElementById("userName").value;
                    var userPW = document.getElementById("userPW").value;

                    //initiate POST request
                    var xhr = new XMLHttpRequest();

                    xhr.open("POST", 'server.js', false);

                    //Send the proper header information along with the request
                    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

                    xhr.onreadystatechange = function() { // Call a function when the state changes.
                        if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
                            // Request finished. Do processing here.
                        }
                    }
                    xhr.send("userName=" + userName + "&userPW=" + userPW);
                }
        </script>
    </body>
</html>

SERVER.js

var http = require('http');
var fs = require('fs');
var qs = require('querystring');
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert').strict;

// Connection URL
const url = 'mongodb://localhost:27017';

// Database Name
const dbName = 'myproject';

// Create a new MongoClient
const client = new MongoClient(url);

    var server = http.createServer(function (req, res) {
        client.connect(function(err) {
            assert.equal(null, err);
            console.log("Connected successfully to server");

            const db = client.db(dbName);

            if(req.method == "GET")
            {
                if(req.url === '/'){ 
                    fs.readFile('home.html', function(err, data) {
                        res.writeHead(200, {'Content-Type': 'text/html'});
                        res.write(data);
                        res.end();
                    });
                }
            }
            else if (req.method == 'POST') {
                var body = '';

                req.on('data', function (data){
                    body += data;
                })

                req.on('end', () => {
                    var postData = qs.parse(body);

                    // Use connect method to connect to the Server


                        // Get the documents collection
                        const collection = db.collection('doc');
                        // Insert some documents
                        collection.insertOne(postData, function(err, result) {
                            console.log(postData);
                            client.close();
                        });

                    res.end();
                })
            }
        });
    });


    server.listen(3000);
    console.log("listening on 3000");
明亮的星星

如上面的评论所述,解决此问题的方法是遵循Mongodb最佳实践,而不是每次将数据写入数据库时​​都关闭客户端的连接。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章