-4

I am creating a simple server using nodejs, expressJs and mongodb

here is server.js

const express        = require('express');
const MongoClient    = require('mongodb').MongoClient;
const bodyParser     = require('body-parser');
const db             = require('./config/db');
const app            = express();
const port = 8000;
app.use(bodyParser.urlencoded({ extended: true }));
MongoClient.connect(db.url, (err, database) => {
  if (err) return console.log(err)
  require('./app/routes')(app, database);
  app.listen(port, () => {
    console.log('We are live on ' + port);
  });               
})

Note: database info are all okay,

when i run node server.js , i get the following error :

    C:\Users\Bonge\Documents\Projects\movies_database>node server.js
{ MongoError: Authentication failed.
    at Function.MongoError.create (C:\Users\Bonge\Documents\Projects\movies_database\node_modules\mongodb-core\lib\error.js:31:11)
    at C:\Users\Bonge\Documents\Projects\movies_database\node_modules\mongodb-core\lib\connection\pool.js:497:72
    at authenticateStragglers (C:\Users\Bonge\Documents\Projects\movies_database\node_modules\mongodb-core\lib\connection\pool.js:443:16)
    at Connection.messageHandler (C:\Users\Bonge\Documents\Projects\movies_database\node_modules\mongodb-core\lib\connection\pool.js:477:5)
    at Socket.<anonymous> (C:\Users\Bonge\Documents\Projects\movies_database\node_modules\mongodb-core\lib\connection\connection.js:331:22)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
    at Socket.Readable.push (_stream_readable.js:208:10)
  name: 'MongoError',
  message: 'Authentication failed.',
  ok: 0,
  errmsg: 'Authentication failed.',
  code: 18,
  codeName: 'AuthenticationFailed' }

what is wrong with my code?

Hot Zellah
  • 1,061
  • 3
  • 11
  • 21
  • 1
    show your db config file and have you checked https://stackoverflow.com/questions/30924859/unable-to-connect-to-mongolab-getting-mongoerror-auth-failed – Gaurav Paliwal Jun 18 '18 at 18:19
  • What is your mongoose and mongodb version? And also the config file – Ashh Jun 18 '18 at 18:26
  • @Ashish mongodb version: "mongodb": "^2.2.33", – Hot Zellah Jun 18 '18 at 18:39
  • @GauravPaliwal `module.exports = { url: "mongodb://dbnameusername:dbpass@ds255260.mlab.com:55260/dbname" }` – Hot Zellah Jun 18 '18 at 18:41
  • @HotZellah Is `dbname` the authentication DB name? Because if not, you shouldn't be passing it.. – Vasan Jun 18 '18 at 18:45
  • make sure your `username`, `password` and `dbname` doesn't contain special characters like `@,-,+,`,+` – Ashh Jun 18 '18 at 18:51
  • 1
    @HotZellah The [doc](https://docs.mongodb.com/manual/reference/connection-string/) says this: _Optional. The name of the database to authenticate if the connection string includes authentication credentials in the form of username:password@. If /database is not specified and the connection string includes credentials, the driver will authenticate to the admin database._ – Vasan Jun 18 '18 at 18:51

1 Answers1

0

i found solution just created new database and new user and everything okay now

Hot Zellah
  • 1,061
  • 3
  • 11
  • 21