简体   繁体   中英

Connecting to the mongodb

I'm new in the MEAN developing, I'm developing a simple app, and for my first step I'm trying to connect to my mongodb, so I installed node, express, morgan,mongodb, mongoose.

So here is my code in index.js:

const express = require('express');
const morgan = require('morgan');
const app = express();

const { MongoClient } = require('./database');

// Settings
app.set('port', process.env.PORT || 3000);


// Middlewares
app.use(morgan('dev'));
app.use(express.json());

// Routes


// Starting the server
app.listen(app.get('port'), () => {
    console.log('server on port', app.get('port'));
});

and then the code on my database.js:

const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://duke:<password>@cluster0-dreyi.mongodb.net/test?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
  const collection = client.db("test").collection("devices");
  console.log("horrorrrrrr");
  // perform actions on the collection object
  client.close();
}); 

module.exports = MongoClient;

I also try this code that is on the mongodb page to connect to the application:

const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://duke:<password>@cluster0-dreyi.mongodb.net/test?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
  const collection = client.db("test").collection("devices");
  // perform actions on the collection object
  client.close();
});

Of course I change the password to the real one. Please keep in my today it's my first time I touch mongodb and also the MEAN full stack, and I spent too many hours stuck in this connection.

this is the error I get:

(node:5284) 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.

EDIT

@iLiA thanks for your reply, I tried your code and ain't working: I will show you how I did it with the real password :

const url = 'mongodb+srv://duke:password@cluster0-dreyi.mongodb.net/test?retryWrites=true&w=majority';

const mongoose = require('mongoose');
mongoose.connect(url, {
   useNewUrlParser: true,
   useCreateIndex: true,
   useUnifiedTopology: true,
   useFindAndModify: false
})
.then(()=>{
    console.log('congrats, problem solved')
})
.catch((err)=>{
    console.log(`there is not a problem with ${err.message}`);
    process.exit(-1)
})



module.exports = mongoose;

and the error is: there is not a problem with Server selection timed out after 30000 ms [nodemon] app crashed - waiting for file changes before starting...

Kind regards,

I am confused about why do you downloaded both mongodb and mongoose but here is mongoose solution

const mongoose = require('mongoose');
mongoose.connect(url, {
   useNewUrlParser: true,
   useCreateIndex: true,
   useUnifiedTopology: true,
   useFindAndModify: false
})
.then(()=>{
    console.log('congrats, problem solved')
})
.catch((err)=>{
    console.log(`there is a problem with ${err.message}`);
    process.exit(-1)
})

EDIT : As it seems you forgot to whitelist your IP address in mongo atlas.

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