简体   繁体   中英

Can't connect to mongoDB Atlas using mongoose

I'm doing a tutorial on Nodejs that uses mongoose to connect to MongoDB Atlas. The problem is that is not connecting to MongoDB.

{ MongoTimeoutError: Server selection timed out after 30000 ms
    at Timeout.setTimeout [as _onTimeout] (/Users/rodrigo/Desktop/node_login/node_modules/mongodb/lib/core/sdam/server_selection.js:308:9)
    at ontimeout (timers.js:436:11)
    at tryOnTimeout (timers.js:300:5)
    at listOnTimeout (timers.js:263:5)
    at Timer.processTimers (timers.js:223:10)
  name: 'MongoTimeoutError',
  reason:
   { MongoError: bad auth Authentication failed.
       at _authenticateSingleConnection (/Users/rodrigo/Desktop/node_login/node_modules/mongodb/lib/core/auth/auth_provider.js:46:25)
       at sendAuthCommand (/Users/rodrigo/Desktop/node_login/node_modules/mongodb/lib/core/auth/scram.js:215:18)
       at _callback (/Users/rodrigo/Desktop/node_login/node_modules/mongodb/lib/core/connection/connect.js:320:5)
       at Connection.messageHandler (/Users/rodrigo/Desktop/node_login/node_modules/mongodb/lib/core/connection/connect.js:349:5)
       at Connection.emit (events.js:189:13)
       at processMessage (/Users/rodrigo/Desktop/node_login/node_modules/mongodb/lib/core/connection/connection.js:368:10)
       at TLSSocket.<anonymous> (/Users/rodrigo/Desktop/node_login/node_modules/mongodb/lib/core/connection/connection.js:537:15)
       at TLSSocket.emit (events.js:189:13)
       at addChunk (_stream_readable.js:284:12)
       at readableAddChunk (_stream_readable.js:265:11)
       at TLSSocket.Readable.push (_stream_readable.js:220:10)
       at TLSWrap.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
     name: 'MongoNetworkError',
     [Symbol(mongoErrorContextSymbol)]: {} },
  [Symbol(mongoErrorContextSymbol)]: {} }

This how the connection is made:

const express = require('express');
const expressLayouts = require('express-ejs-layouts');
const mongoose = require('mongoose');


const app = express();

// DB Config
// url : mongodb+srv://rodrigo:<rodrigo>@recifeprev-xv52e.mongodb.net/test?retryWrites=true&w=majority
const db = require('./config/keys').MongoURI;

//Connect to mongo
mongoose.connect(db, {useNewUrlParser: true, useUnifiedTopology:true})
    .then(() => console.log('MongoDB Connected...'))
    .catch(err => console.log(err));



// EJS
app.use(expressLayouts);
app.set('view engine', 'ejs');

// Routes
app.use('/' , require('./routes/index'));
app.use('/users' , require('./routes/users'));

const PORT = process.env.PORT || 27017;


app.listen(PORT, console.log(`Server started on port ${PORT}`));

I am using my machine ip and checked the ip whitelist on mogoDB.

问题是密码网址上的“<”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM