43

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

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 :

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.

Jerry Chong
  • 7,954
  • 4
  • 45
  • 40
Ahmed202
  • 737
  • 3
  • 7
  • 12
  • 1
    You may be following an old tutorial or documentation. That used to work but it has changed to requiring mongodb:// – Adam Mendoza Nov 13 '16 at 00:36

9 Answers9

125

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

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

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

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

Ajay Gupta
  • 1,944
  • 2
  • 21
  • 36
Jonathan Muller
  • 7,348
  • 2
  • 23
  • 31
8

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
Mike Cheel
  • 12,626
  • 10
  • 72
  • 101
6

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
prodeveloper
  • 943
  • 14
  • 22
  • This seems to be the case on WIndows 10 at least, haven't tested other systems. Thanks! – user25794 Aug 27 '17 at 08:50
  • 2
    Just had the same issue with `docker-compose` environment variables. I needed to not have it wrapped in any quotes (single or double). This worked for me. `MONGODB_URI=mongodb://mongo:27017/` – Josh Peak May 29 '18 at 01:14
  • Using the mongo-seeding-cli (https://github.com/pkosiec/mongo-seeding/tree/master/cli). This was the issue for me. Their example for specifying the host includes quoting. However, including the quotes seems to cause OP's error. – BRasmussen May 05 '19 at 17:22
2

Try this, it works:

mongoose.connect('mongodb://localhost:27017/shopping');
Manohar Reddy Poreddy
  • 25,399
  • 9
  • 157
  • 140
1

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.

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

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

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

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

the working code would be like this

don't forget to replace 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);
        });


    });

});
Jamil Noyda
  • 3,321
  • 2
  • 21
  • 26
0

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.

Adam Reis
  • 4,165
  • 1
  • 44
  • 35
0

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.

Jerry Chong
  • 7,954
  • 4
  • 45
  • 40
0

update your mongodb npm version

itzhar
  • 12,743
  • 6
  • 56
  • 63