Mongodb:不支持选项[…]

编程入门 行业动态 更新时间:2024-10-27 04:35:38

Mongodb:<a href=https://www.elefans.com/category/jswz/34/1771331.html style=不支持选项[…]"/>

Mongodb:不支持选项[…]

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

已成功连接到服务器将3个文档插入到收集选项[服务器]不支持这些选项不支持[caseTranslate]选项[dbName]不支持已成功连接到服务器将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最佳实践,而不是每次将数据写入数据库时​​都关闭客户端的连接。

更多推荐

Mongodb:不支持选项[…]

本文发布于:2024-05-07 00:56:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1754025.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不支持   选项   Mongodb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!