Mongodb:不支持选项 [...] [英] Mongodb: the Options [...] is not supported

查看:61
本文介绍了Mongodb:不支持选项 [...]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<块引用>

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

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

HTML

<头><标题>小结构<身体><div id="演示"><h1>点击这里</h1><button type="button" onclick="loadDoc()">提交</button></br><input id = "userName"></input></br><input id = "userPW" type = "password"></input></br>

<脚本>函数加载文档(){//获取用户值var userName = document.getElementById("userName").value;var userPW = document.getElementById("userPW").value;//发起POST请求var xhr = new XMLHttpRequest();xhr.open("POST", 'server.js', false);//随请求一起发送正确的头信息xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");xhr.onreadystatechange = function() {//当状态改变时调用一个函数.if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {//请求完成.在这里做处理.}}xhr.send("userName=" + userName + "&userPW=" + userPW);}</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;//连接地址const url = 'mongodb://localhost:27017';//数据库名称const dbName = 'myproject';//创建一个新的 MongoClientconst client = new MongoClient(url);var server = http.createServer(function (req, res) {客户端连接(功能(错误){assert.equal(null, err);console.log("连接服务器成功");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(数据);重发();});}}否则 if (req.method == 'POST') {var 身体 = '';req.on('数据', 函数(数据){正文 += 数据;})req.on('end', () => {var postData = qs.parse(body);//使用connect方法连接到服务器//获取文档集合const collection = db.collection('doc');//插入一些文件collection.insertOne(postData, function(err, result) {控制台日志(postData);客户端关闭();});重发();})}});});服务器.听(3000);console.log("监听 3000");

解决方案

正如上面评论中所讨论的,这个问题的解决方案是遵循 Mongodb 最佳实践,并且每次将数据写入数据库时​​都不要关闭客户端的连接.

Here I have a server and an html file making an ajax call. I want to put the username and password inside the database when posted. When I FIRST load up the client and send the information, it goes into the database successfully, but if i send another batch of information I get an ERROR: the following is the error:

Connected successfully to server Inserted 3 documents into the collection the options [servers] is not supported the options [caseTranslate] is not supported the options [dbName] is not supported Connected successfully to server Inserted 3 documents into the collection

And My database will no longer populate until i restart the server. Can someone please point me in the right direction? could it be the way I've structured the client connect inside the .on POST event?

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");

解决方案

As discussed in comments above, the solution to this problem was to follow Mongodb best practices and not close the client's connection each time data was to be written to the database.

这篇关于Mongodb:不支持选项 [...]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆