【问题标题】:MongoDB - Error: invalid schema, expected mongodbMongoDB - 错误:无效的架构,预期的 mongodb
【发布时间】:2016-06-19 05:21:26
【问题描述】:

我是使用 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);
});

});

});

我确定我创建了一个名为 chat with mongodb 的数据库,mongo 也在等待连接。但是当我使用节点 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>

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

谢谢。

【问题讨论】:

  • 您可能正在关注旧的教程或文档。以前可以工作,但现在改为需要 mongodb://

标签: node.js mongodb mean-stack


【解决方案1】:

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

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

连接字符串的模式是mongodb://&lt;HOSTNAME&gt;:&lt;PORT&gt;/&lt;DBNAME&gt;

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

【讨论】:

  • 在 MongoDB 3.6.2 和 Ubuntu 16.04 中运行良好
  • nosqlclient的用户请注意,它要求url以mongdb://开头,localhost这样的url是不行的。
【解决方案2】:

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

mongo://localhost:27017/test

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

mongodb://localhost:27017/test

【讨论】:

    【解决方案3】:

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

    错误可能与:

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

    正确的命令是:

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

    【讨论】:

    • 至少在 WIndows 10 上似乎是这种情况,还没有测试过其他系统。谢谢!
    • 刚刚遇到了与docker-compose 环境变量相同的问题。我不需要将它包含在任何引号中(单引号或双引号)。这对我有用。 MONGODB_URI=mongodb://mongo:27017/
    • 使用 mongo-seeding-cli (github.com/pkosiec/mongo-seeding/tree/master/cli)。这对我来说是个问题。他们指定主机的示例包括引用。但是,包括引号似乎会导致 OP 的错误。
    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

      试试这个,它有效:

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

      【讨论】:

        【解决方案6】:

        刚刚发现同样的问题。该死的窗口在环境中保存引号。

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

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

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

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

        【讨论】:

          【解决方案7】:

          改变这一行的内容

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

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

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

          【讨论】:

            【解决方案8】:

            工作代码是这样的

            别忘了替换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);
                    });
            
            
                });
            
            });
            

            【讨论】:

              【解决方案9】:

              更新你的 mongodb npm 版本

              【讨论】:

                猜你喜欢
                • 2019-11-07
                • 1970-01-01
                • 1970-01-01
                • 2016-02-17
                • 2018-03-16
                • 2015-05-22
                • 1970-01-01
                • 2014-02-04
                • 2012-05-05
                相关资源
                最近更新 更多