1

my server.js file

  MongoClient.connect('mongodb://user:password@127.0.0.1:27017/QUIZ_GAME',(err,client)=>{
if(err){
  return console.log('unable to connect to the MongoDb server');
}
 const db =client.db('QUIZ_GAME');
 console.log('connected to the MongoDb server');

in my putty terminal i had done this, to enable auth and iwas successful in doing it

 use admin
db.createUser(
{
user: "admin",
pwd: "password",
roles: [ { role: "root", db: "admin" } ]
 }
);
exit;

 use QUIZ_GAME
 db.createUser(
{
  user: "tester",
  pwd: "password",
  roles: [{ role: 'readWrite', db:'QUIZ_GAME'}]
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
roh_dev
  • 275
  • 2
  • 6
  • 17
  • just use `mongodb://localhost/QUIZ_GAME` to connect locally – Shaishab Roy Apr 23 '18 at 10:13
  • Just `'mongodb://user:password@127.0.0.1:27017` and leave the database name off the connection. The javascript driver changed at version 3.x to be like all the other drivers. In all other drivers, that database name "on the connection string" is the one used for "authentication". When you **omit it***, then the driver looks in `admin` to authenticate. And that is where you *should **always*** be storing the users you create. – Neil Lunn Apr 23 '18 at 10:14
  • Just to be clear, your user creation as shown in the question is completely correct. It's just where you are pointing the driver to authenticate against where the problem is. – Neil Lunn Apr 23 '18 at 10:16
  • sorry@NeilLunn i didnt get u – roh_dev Apr 23 '18 at 10:18
  • `MongoClient.connect('mongodb://user:password@127.0.0.1:27017',(err,client)=>{`. Everything else is okay and that is the only thing wrong. – Neil Lunn Apr 23 '18 at 10:25
  • that didnt solve my problem. .when i use npm start in my putty , i get the same error – roh_dev Apr 23 '18 at 10:28
  • `MongoClient.connect('mongodb://tester:password@127.0.0.1:27017',(err,client)=>{`. Since you actually told us what the user and password are, then simply copy the code from me, Don't change anything, just use what I typed here on that line. I've supplied the user and password, so you should not need to change that and there is **NO DATABASE** on the end. And that's actually the whole point. – Neil Lunn Apr 23 '18 at 10:30
  • That was an example of the password. Btw my password contains @as well like abc@ is my password. is this making the difference? – roh_dev Apr 23 '18 at 10:36
  • And btw i am getting the same error even after removing the database name as u mentioned – roh_dev Apr 23 '18 at 10:38
  • having the same error please help on this. https://stackoverflow.com/questions/51806349/node-js-mongodb-connection-error-mongodb-errormongoerror-there-are-no-users – Akshay Champavat Aug 12 '18 at 06:48

2 Answers2

0

Add your data bearing nodes

rs.initiate( {
   _id : "anyId" ,
   members: [ { _id : 0, host : "IP:PORT" } ]
})

In future if you want to add any other member then add rs.add("ip:port") and then try this

Tatkal
  • 568
  • 4
  • 9
  • 29
  • The rs.initiate() method provides a wrapper around the replSetInitiate command. repl stands for replica – Tatkal Apr 23 '18 at 10:26
-1

To connect to the server just use

db: 'mongodb://username:password@10.2.1.00/db_name'