node.js MongoDB - 错误:无效架构,需要 mongodb

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35813584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:01:17  来源:igfitidea点击:

MongoDB - Error: invalid schema, expected mongodb

node.jsmongodbmean-stack

提问by Ahmed202

I'm new in building application with MEAN Stack, I'm trying to build a real time chat app, here is my server side :

我是使用 MEAN Stack 构建应用程序的新手,我正在尝试构建一个实时聊天应用程序,这是我的服务器端:

console.log("Server running...!");

var mongo=require('mongodb').MongoClient;
var client=require('socket.io').listen(8080).sockets;

mongo.connect('localhost:27017/db/chat',function(err,db){
if(err)  throw err;

client.on('connection',function(socket){
console.log('someone has connected !');

//waiting for input
socket.on('input',function(data){
console.log(data);
});

});

});

I am sure that i created a data base called chat with mongodb, also mongo is waiting for connection. But when i run the server with node server.js an error occurs :

我确定我创建了一个名为 chat with mongodb 的数据库,mongo 也在等待连接。但是当我使用 node server.js 运行服务器时,会发生错误:

Server running...!
C:\Users\azus\Desktop\Psirt\codemaster\node_modules\ mongodb\lib\url_parser.js:20
  throw new Error('invalid schema, expected mongodb');
  ^

Error: invalid schema, expected mongodb
at module.exports (C:\Users\azus\Desktop\Psirt\code-master\node_modules\mong
 odb\lib\url_parser.js:20:11)
at connect (C:\Users\azus\Desktop\Psirt\code-master\node_modules\mongodb\lib
 \mongo_client.js:125:16)
at Function.MongoClient.connect (C:\Users\azus\Desktop\Psirt\code-master\nod
e_modules\mongodb\lib\mongo_client.js:109:3)
at Object.<anonymous> (C:\Users\azus\Desktop\Psirt\code-master\server.js:6:8
)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:139:18)

C:\Users\azus\Desktop\Psirt\code-master>

I had been blocked at this phase for weeks, could anyone help on this?

我在这个阶段被阻止了几个星期,有人可以帮忙吗?

Thanks.

谢谢。

回答by Jonathan Muller

This is because you are using the connection string in an improper format.

这是因为您使用的连接字符串格式不正确。

You are using localhost:27017/db/chatwhile it should be mongodb://localhost:27017/db/chat

您正在使用,localhost:27017/db/chat而它应该是mongodb://localhost:27017/db/chat

The pattern for the connection string is mongodb://<HOSTNAME>:<PORT>/<DBNAME>

连接字符串的模式是 mongodb://<HOSTNAME>:<PORT>/<DBNAME>

Article for reference: https://mongodb.github.io/node-mongodb-native/api-generated/mongoclient.html#mongoclient-connect

参考文章:https: //mongodb.github.io/node-mongodb-native/api-generated/mongoclient.html#mongoclient-connect

回答by Mike Cheel

I just had this issue as well and it was because I had the protocol wrong:

我也遇到了这个问题,这是因为我的协议有误:

mongo://localhost:27017/test

The protocol being wrong can also cause this error. It should be like this:

协议错误也会导致此错误。应该是这样的:

mongodb://localhost:27017/test

回答by prodeveloper

Sometimes, error might be with the quotes around environment variables. Remove them once and try. Might help.

有时,错误可能与环境变量周围的引号有关。删除它们一次并尝试。可能有帮助。

Error might be with :

错误可能与:

 set DATABASE_URI='mongodb://localhost:1000/my_app' && node index.js

Correct command will be:

正确的命令将是:

  set DATABASE_URI=mongodb://localhost:1000/my_app && node index.js

回答by Mukolo Patrick Ejiro

Try this, it works:

试试这个,它有效:

mongoose.connect('mongodb://localhost:27017/shopping');

回答by Ivan Ivan

Just figured out the same problem. Damned windows save quotes in environment.

刚刚想出了同样的问题。该死的窗口在环境中保存报价。

So if you use windows and wrote this way SET MONGO_URL="mongodb://localhost:27017/{name of your db}"It is not correct.

所以如果你用windows这样写SET MONGO_URL="mongodb://localhost:27017/{name of your db}"是不对的。

Correct way is SET MONGO_URL=mongodb://localhost:27017/{name of your db}without quotes.

正确的方法是SET MONGO_URL=mongodb://localhost:27017/{name of your db}不加引号。

Also i discovered that you must write protocol exactly - mongodb. There is code what check the protocol from file url_parser.js

我还发现您必须准确地编写协议 - mongodb。有代码从文件 url_parser.js 检查协议

var result = parser.parse(url, true);

if(result.protocol != 'mongodb:') {
    throw new Error('invalid schema, expected mongodb');
}

回答by Jerry Chong

Change content of this line from

将这一行的内容从

mongo.connect('localhost:27017/db/chat',function(err,db)

to

mongo.connect('mongodb://localhost:27017/db/chat',function(err,db)

Then you can connect MongoDB database successfully.

然后就可以成功连接MongoDB数据库了。

回答by Jamil Noyda

the working code would be like this

工作代码是这样的

don't forget to replace username, password& URL

不要忘记替换username, password&URL

const socketClient = require('socket.io').listen(4000).sockets;
const MongoClient = require('mongodb').MongoClient;

const uri = "mongodb+srv://<username>:<password>@cluster0-saugt.mongodb.net/test?retryWrites=true&w=majority";

const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
    socketClient.on('connection', function (socket) {

        //Need to Get the Database first before trying to access the collections.
        let chat = client.db("test").collection('chats');

        // Get chats from mongo collection
        // perform actions on the collection object
        chat.find().limit(100).sort({ _id: 1 }).toArray(function (err, res) {
            if (err) {
                throw err;
            }

            // Emit the messages
            socket.emit('output', res);
        });


    });

});

回答by Adam Reis

Might seem obvious, but you'll also encounter this error when you pass invalid values in general to the mongo client, e.g. undefined. Ran into this when I was referencing the wrong key on a config object.

可能看起来很明显,但是当您通常将无效值传递给 mongo 客户端时,您也会遇到此错误,例如undefined. 当我在配置对象上引用错误的键时遇到了这个问题。