MongoDB连接上的身份验证错误

时间:2019-03-05 11:32:33

标签: node.js mongodb mongodb-atlas

你好,我正在尝试将我的项目与mongo地图集联系起来,但无法正常工作

在控制台上显示此错误:

  

((node:17810)DeprecationWarning:不建议使用当前的URL字符串解析器,并将在以后的版本中删除。要使用新的解析器,请将选项{useNewUrlParser:true}传递给MongoClient.connect。       {MongoError:身份验证失败)

(node:17810) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
{ MongoError: authentication fail
    at /home/chromosome/Documents/chromeo/node_modules/mongodb-core/lib/topologies/replset.js:1458:15
    at /home/chromosome/Documents/chromeo/node_modules/mongodb-core/lib/connection/pool.js:869:7
    at /home/chromosome/Documents/chromeo/node_modules/mongodb-core/lib/connection/pool.js:845:20
    at finish (/home/chromosome/Documents/chromeo/node_modules/mongodb-core/lib/auth/scram.js:233:16)
    at handleEnd (/home/chromosome/Documents/chromeo/node_modules/mongodb-core/lib/auth/scram.js:243:7)
    at /home/chromosome/Documents/chromeo/node_modules/mongodb-core/lib/auth/scram.js:352:15
    at /home/chromosome/Documents/chromeo/node_modules/mongodb-core/lib/connection/pool.js:532:18
    at processTicksAndRejections (internal/process/next_tick.js:74:9)
  errors:
   [ { name: 'cluster0-shard-00-00-pml0t.mongodb.net:27017',
       err: [MongoError] } ],
  name: 'MongoError',
  [Symbol(mongoErrorContextSymbol)]: {} }

这是我连接数据库的密码

    const path = require('path');
    const express = require('express');
    const bodyParser = require('body-parser');
    const mongoose = require('mongoose');

    const errorController = require('./controllers/error');
    const User = require('./models/user');

    const app = express();

    app.set('view engine', 'ejs');
    app.set('views', 'views');

    const adminRoutes = require('./routes/admin');
    const shopRoutes = require('./routes/shop');

    app.use(bodyParser.urlencoded({ extended: false }));
    app.use(express.static(path.join(__dirname, 'public')));

    app.use((req, res, next) => {
      User.findById('5bab316ce0a7c75f783cb8a8')
        .then(user => {
          req.user = user;
          next();
        })
        .catch(err => console.log(err));
    });

    app.use('/admin', adminRoutes);
    app.use(shopRoutes);

    app.use(errorController.get404);


//mogodb connection logic
    mongoose
      .connect(
    'mongodb+srv://chromosome:__password@cluster0-pml0t.mongodb.net/test?retryWrites=true')
      .then(result => {
        User.findOne().then(user => {
          if (!user) {
            const user = new User({
              name: 'Max',
              email: 'max@test.com',
              cart: {
                items: []
              }
            });
            user.save();
          }
        });
        app.listen(5000, () =>{
          console.log(`listinig to port number 5000`)
        });
      })
      .catch(err => {
        console.log(err);
      });

0 个答案:

没有答案