连接到mongodb时遇到麻烦

时间:2020-06-11 20:35:36

标签: node.js mongodb express mongoose

我正在学习教程并尝试连接到mongodb,我有两个文件 第一个是具有以下代码的server.js

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

const app = express();

//body parser middleware
app.use(bodyParser.json());

//DB config

const db = require('./config/keys').mongoURI;

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

const port = process.env.PORT || 5000;

app.listen(port,()=>console.log(`server connected on port ${port}`));

第二个是配置文件

module.exports = {
    mongoURI:'mongodb+srv://denis:password@shoppingapp-wmnbw.mongodb.net/shopping?retryWrites=true&w=majority'
}

我想要实现的是在控制台connected to mongodb...上显示 相反,我收到此错误消息

server connected on port 5000
(node:35030) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
MongoNetworkError: failed to connect to server [shoppingapp-shard-00-01-wmnbw.mongodb.net:27017] on first connect [MongoNetworkError: connection 5 to shoppingapp-shard-00-01-wmnbw.mongodb.net:27017 closed
    at TLSSocket.<anonymous> (/home/denis/Desktop/projects/MERN_SHOPPING_LIST/node_modules/mongodb/lib/core/connection/connection.js:372:9)
    at Object.onceWrapper (events.js:417:26)
    at TLSSocket.emit (events.js:310:20)
    at net.js:672:12
    at TCP.done (_tls_wrap.js:557:7)]
    at Pool.<anonymous> (/home/denis/Desktop/projects/MERN_SHOPPING_LIST/node_modules/mongodb/lib/core/topologies/server.js:438:11)
    at Pool.emit (events.js:310:20)
    at /home/denis/Desktop/projects/MERN_SHOPPING_LIST/node_modules/mongodb/lib/core/connection/pool.js:561:14
    at /home/denis/Desktop/projects/MERN_SHOPPING_LIST/node_modules/mongodb/lib/core/connection/pool.js:1008:9
    at callback (/home/denis/Desktop/projects/MERN_SHOPPING_LIST/node_modules/mongodb/lib/core/connection/connect.js:97:5)
    at /home/denis/Desktop/projects/MERN_SHOPPING_LIST/node_modules/mongodb/lib/core/connection/connect.js:124:7
    at _callback (/home/denis/Desktop/projects/MERN_SHOPPING_LIST/node_modules/mongodb/lib/core/connection/connect.js:349:5)
    at Connection.errorHandler (/home/denis/Desktop/projects/MERN_SHOPPING_LIST/node_modules/mongodb/lib/core/connection/connect.js:365:5)
    at Object.onceWrapper (events.js:417:26)
    at Connection.emit (events.js:310:20)
    at TLSSocket.<anonymous> (/home/denis/Desktop/projects/MERN_SHOPPING_LIST/node_modules/mongodb/lib/core/connection/connection.js:370:12)
    at Object.onceWrapper (events.js:417:26)
    at TLSSocket.emit (events.js:310:20)
    at net.js:672:12
    at TCP.done (_tls_wrap.js:557:7)

我试图更改驱动程序版本,但仍然出现错误,我在哪里出错?

1 个答案:

答案 0 :(得分:0)

事实证明我没有使用正确的IP地址进行连接。