I’m having problems connecting to my mongoDB Database JavaScript

Hey Guys ! I’m building a simple CRUD operation using Mongodb, Here’s the code:

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

app.listen(4000,function () {
    console.log('Server is listening on port 4000');
})

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

app.get('/',(req,res) => {
    // res.send('Hello World');
    res.sendFile(__dirname + '/index.html');
})
app.post('/quotes',(req,res) => {
    console.log(req.body);
})

const MongoClient = require('mongodb').MongoClient
MongoClient.connect('connectionString',{useUnifiedTopology: true, useNewUrlParser: true },(err,client)=>{
    if (err) return console.error(err);
        console.log('Connected to database')
})

> And I don’t know how to remove this error:

[nodemon] starting node server.js
MongoParseError: Invalid connection string
at parseConnectionString (C:\Users\Win 10 Pro\Desktop\JavaScript-basics\OOP in JS\node_modules\mongodb\lib\core\uri_parser.js:565:21)
at connect (C:\Users\Win 10 Pro\Desktop\JavaScript-basics\OOP in JS\node_modules\mongodb\lib\operations\connect.js:282:3)
at C:\Users\Win 10 Pro\Desktop\JavaScript-basics\OOP in JS\node_modules\mongodb\lib\mongo_client.js:224:5
at maybePromise (C:\Users\Win 10 Pro\Desktop\JavaScript-basics\OOP in JS\node_modules\mongodb\lib\utils.js:665:3)
at MongoClient.connect (C:\Users\Win 10 Pro\Desktop\JavaScript-basics\OOP in JS\node_modules\mongodb\lib\mongo_client.js:220:10)
at Function.MongoClient.connect (C:\Users\Win 10 Pro\Desktop\JavaScript-basics\OOP in JS\node_modules\mongodb\lib\mongo_client.js:443:2

Replace connectionString with the port of mongoDB

This the docs for reference
https://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html

I don’t understand …

#Mohamed-Magdey !! I replace the >MongoClient.connect(‘connectionString’,
with:

const MongoClient = require('mongodb').MongoClient;

MongoClient.connect('mongodb://localhost:27017',{useUnifiedTopology: true },(err,client)=>{

    if (err) return console.error(err);

        console.log('Connected to database');

})

But its either not working !! and giving another error

You should do something like this:
MongoClient.connect("mongodb://localhost:27017/integration_test", function(err, db)

Error:
[nodemon] starting node server.js
Server is listening on port 4000
MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
at Timeout._onTimeout (C:\Users\Win 10 Pro\Desktop\JavaScript-basics\OOP in JS\node_modules\mongodb\lib\core\sdam\topology.js:438:30)
at listOnTimeout (internal/timers.js:549:17)
at processTimers (internal/timers.js:492:7) {
reason: TopologyDescription {
type: ‘Unknown’,
setName: null,
maxSetVersion: null,
maxElectionId: null,
servers: Map { ‘localhost:27017’ => [ServerDescription] },
stale: false,
compatible: true,
[nodemon] restarting due to changes…
[nodemon] starting node server.js
Server is listening on port 4000
MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
at Timeout._onTimeout (C:\Users\Win 10 Pro\Desktop\JavaScript-basics\OOP in JS\node_modules\mongodb\lib\core\sdam\topology.js:438:30)
at listOnTimeout (internal/timers.js:549:17)
at processTimers (internal/timers.js:492:7) {
reason: TopologyDescription {
type: ‘Unknown’,
setName: null,
maxSetVersion: null,
maxElectionId: null,
servers: Map { ‘localhost:27017’ => [ServerDescription] },
stale: false,
compatible: true,
compatibilityError: null,
logicalSessionTimeoutMinutes: null,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
commonWireVersion: null
}
}

Do you work with mongoDB locally?

I just newly start doing work with Mongo … couple a days ago … and I’m just implementing the things.
I’m using cloud mongodb atlas right now

Then the url should refer to that atlas not to the localhost.

how to implement url ?

Did you create culster?

Yes I create that !!!

Then follow this step https://docs.atlas.mongodb.com/connect-to-cluster/
and put the ip to 0.0.0.0 so you can access to the DB from anyware.
After you click connect choose connect your application and the url you want will appear.

1 Like

I can’t resolve my problem :frowning:

What is the problem?

Finally the problem get solved !!! :slight_smile:

Thank you #Mohamed-Magdey

You are welcome :slight_smile:.

Sign the solution so the topic appear as solved.