Link to home
Start Free TrialLog in
Avatar of chalie001
chalie001

asked on

fail to connect to mongo db

hi am geting this error when trying to connect to my mongo
nodemon] starting `node server.js`
(node:6648) 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.
(node:6648) 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.
Connection failed!
[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
(node:17788) 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.
Connection failed!

this my connection code
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");

const Post = require("./models/post");

const app = express();

const connectUrl = 'mongodb+srv://username:yxchH2sxBVh46LHB@mongodbcl-ws2rs.mongodb.net/test?retryWrites=true&w=majority';
 
const connectConfig = { 
  useNewUrlParser: true, 
  
}


mongoose.connect(connectUrl, connectConfig)
  .then(() => {
    console.log("Connected to database!");
  })
  .catch(() => {
    console.log("Connection failed!");
  });



app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.use((req, res, next) => {
  res.setHeader("Access-Control-Allow-Origin", "*");
  res.setHeader(
    "Access-Control-Allow-Headers",
    "Origin, X-Requested-With, Content-Type, Accept"
  );
  res.setHeader(
    "Access-Control-Allow-Methods",
    "GET, POST, PATCH, DELETE, OPTIONS"
  );
  next();
});

app.post("/api/posts", (req, res, next) => {
  const post = new Post({
    title: req.body.title,
    content: req.body.content
  });
  post.save().then(createdPost => {
    res.status(201).json({
      message: "Post added successfully",
      postId: createdPost._id
    });
  });
});

app.get("/api/posts", (req, res, next) => {
  Post.find().then(documents => {
    res.status(200).json({
      message: "Posts fetched successfully!",
      posts: documents
    });
  });
});

app.delete("/api/posts/:id", (req, res, next) => {
  Post.deleteOne({ _id: req.params.id }).then(result => {
    console.log(result);
    res.status(200).json({ message: "Post deleted!" });
  });
});

module.exports = app;

Open in new window

Avatar of leakim971
leakim971
Flag of Guadeloupe image

lines 21,22,23 :

  .catch((err) => {     
       console.log("Connection failed!", err.stack);   // so you will know more about why it fail
   });

Open in new window

Avatar of chalie001
chalie001

ASKER

am geting
ode:25868) 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.
Connection failed! MongoNetworkError: failed to connect to server [mongodbcl-shard-00-01-ws2rs.mongodb.net:27017] on first connect [MongoNetworkError: connection 5 to mongodbcl-shard-00-01-ws2rs.mongodb.net:27017 closed]
    at Pool.<anonymous> (C:\solution2\mean\mongodb-03-finished\node_modules\mongodb\lib\core\topologies\server.js:438:11)
    at Pool.emit (events.js:198:13)
    at createConnection (C:\solution2\mean\mongodb-03-finished\node_modules\mongodb\lib\core\connection\pool.js:561:14)
    at connect (C:\solution2\mean\mongodb-03-finished\node_modules\mongodb\lib\core\connection\pool.js:1008:9)
    at callback (C:\solution2\mean\mongodb-03-finished\node_modules\mongodb\lib\core\connection\connect.js:97:5)
    at runCommand (C:\solution2\mean\mongodb-03-finished\node_modules\mongodb\lib\core\connection\connect.js:124:7)
    at _callback (C:\solution2\mean\mongodb-03-finished\node_modules\mongodb\lib\core\connection\connect.js:349:5)
    at Connection.errorHandler (C:\solution2\mean\mongodb-03-finished\node_modules\mongodb\lib\core\connection\connect.js:365:5)
    at Object.onceWrapper (events.js:286:20)
    at Connection.emit (events.js:198:13)
    at TLSSocket.<anonymous> (C:\solution2\mean\mongodb-03-finished\node_modules\mongodb\lib\core\connection\connection.js:370:12)    at Object.onceWrapper (events.js:286:20)
    at TLSSocket.emit (events.js:198:13)
    at _handle.close (net.js:606:12)
    at TCP.done (_tls_wrap.js:388:7)
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
its in cloud i did change ip it work
don't forget to close the question, I believe this comment << you should be sure about the connexion between your server and it >> describe your issue