I am starting to learn a MERN stack tutorial. I initially created a react app, added a directory called backend, installed express, cors, express, and dotenv, then created a server.js and added some code, but after connecting to mongodb atlas, I get the error below:
(node:5828) UnhandledPromiseRejectionWarning: MongoTimeoutError: Server selection timed out after 30000 ms at Timeout. (C:\Users\Lenovo\Desktop\REACTion\mernapp\backend\node_modules\mongodb\lib\core\sdam\topology.js:878:9) at listOnTimeout (internal/timers.js:531:17) at processTimers (internal/timers.js:475:7) (node:5828) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:5828) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. [nodemon] restarting due to changes... [nodemon] starting
node server.js
Server is running on port: 5000 Connection failed! MongoTimeoutError: Server selection timed out after 30000 ms at Timeout. (C:\Users\Lenovo\Desktop\REACTion\mernapp\backend\node_modules\mongodb\lib\core\sdam\topology.js:878:9) at listOnTimeout (internal/timers.js:531:17) at processTimers (internal/timers.js:475:7) { name: 'MongoTimeoutError', reason: MongoNetworkError: connection 2 to cluster0-shard-00-01-l6mnq.gcp.mongodb.net:27017 closed at TLSSocket. (C:\Users\Lenovo\Desktop\REACTion\mernapp\backend\node_modules\mongodb\lib\core\connection\connection.js:356:9) at Object.onceWrapper (events.js:288:20) at TLSSocket.emit (events.js:200:13) at net.js:586:12 at TCP.done (_tls_wrap.js:479:7) { name: 'MongoNetworkError', errorLabels: [ 'TransientTransactionError' ], [Symbol(mongoErrorContextSymbol)]: {} }, [Symbol(mongoErrorContextSymbol)]: {} }
Below is my code for the server.js file. I copied the uri from MongoDB atlas and pasted it in a .env file.
const express = require('express');
const cors = require('cors');
const mongoose = require('mongoose');
require('dotenv').config();
const app = express();
const port = process.env.PORT || 5000;
app.use(cors());
app.use(express.json());
const uri = process.env.ATLAS_URI;
mongoose
.connect(uri, { useUnifiedTopology: true, useNewUrlParser: true, useCreateIndex: true })
.then(() => {
console.log('Connected to database!');
})
.catch(error => {
console.log('Connection failed!');
console.log(error);
});
const connection = mongoose.connection;
connection.once('open', () => {
console.log('MongoDB database connection established successfully');
});
app.listen(port, () => {
console.log(`Server is running on port: ${port}`);
});
Have you well installed mongoDB in local ?
https://docs.mongodb.com/manual/administration/install-community/
Verify the contents of the system environment variables, from a new console window type the command "echo %ATLAS_URI%", if the URI does not appear it is necessary to add it:
- In Search, search for and then select: System (Control Panel).
- Click the Advanced system settings link.
- Click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit If the PATH environment variable does not exist, click New.
- In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK Close all remaining windows by clicking OK.
If you already have it added it is necessary to restart the computer and try the "echo %ATLAS_URI%" command.
来源:https://stackoverflow.com/questions/58568275/cannot-connect-to-mongdb-atlas-using-mongoose