MongoDB通过Mlab - MongoError:身份验证失败

时间:2018-06-18 18:11:06

标签: node.js mongodb express mongoose

我正在使用nodejs,expressJs和mongodb

创建一个简单的服务器

这是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);
  });               
})

注意:数据库信息都可以,

当我运行node server.js时,我收到以下错误:

    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' }

我的代码出了什么问题?

1 个答案:

答案 0 :(得分:0)

我发现解决方案刚刚创建了新数据库和新用户,现在一切正常