5
const express = require('express')
const app = express()   //initialised express. using express by requiring it.
//conecting the server to browsers
const bodyParser = require('body-parser')
const MongoClient = require('mongodb').MongoClient

var db

MongoClient.connect('mongodb://aryan:aryan@ds127938.mlab.com:27938/post-quotes', 
                    (err, database) => {
    console.log('inside')
    //We move app.listen() so that it boots up only when our database is connected; ony aftert that should it flag: listening
    if (err) return console.log(err)

    db = database
    app.listen(process.env.PORT || 3000, function()  {
    console.log('listening on 3000')
})
    db.collection('quotes').find().toArray( (err, results) => {
        console.log(results)
    })
})

app.use(bodyParser.urlencoded({extended: true}))
//enabling body parser to handle formms as in our case




app.get('/', (req, res) => {
    res.sendFile('/media/aryan/Adani/zellwk' + '/index.html')
})  // /index.html specifies that index.html is stored in the root of your project directory.

app.post('/quotes', (req, res) => {
    db.collection('quotes').save(req.body, (err, result) =>{    //creating a mongodb Collection called quotes and usiing .save silmultaneously to save it on the mongodb server
        if (err) return console.log(err)

        console.log("Save Successful")
        res.redirect('/')   // Now what after the user presses submit. We need to show him som echanges. So  redirect him to the starting page. 

    })



})

In the log screen i get an error:

MongoError: failed to connect to server [ds047955.mongolab.com:47955] on first connect

Here is the complete log:

inside { MongoError: failed to connect to server [ds127938.mlab.com:27938] on first connect at Pool. (/media/aryan/Adani/zellwk/node_modules/mongodb-core/lib/topologies/server.js:309:35) at emitOne (events.js:96:13) at Pool.emit (events.js:188:7) at Connection. (/media/aryan/Adani/zellwk/node_modules/mongodb-core/lib/connection/pool.js:270:12) at Connection.g (events.js:292:16) at emitTwo (events.js:106:13) at Connection.emit (events.js:191:7) at Socket. (/media/aryan/Adani/zellwk/node_modules/mongodb- core/lib/connection/connection.js:185:10) at Socket.g (events.js:292:16) at emitNone (events.js:86:13) name: 'MongoError', message: 'failed to connect to server [ds127938.mlab.com:27938] on first connect' }

DimaSan
  • 12,264
  • 11
  • 65
  • 75
Aryan
  • 51
  • 1
  • 1
  • 4
  • did you get any other answer for this question? – radio_head Feb 07 '17 at 15:12
  • No! None of the many threads that I scanned, worked. – Aryan Feb 12 '17 at 11:35
  • The link to the GitHub repo [Click Here](https://github.com/MaximumEndurance/Blog-MongoDB) and the error log [Click Here](https://github.com/MaximumEndurance/Blog-MongoDB/blob/master/Error%20log)... Thanks for considering. – Aryan Feb 12 '17 at 18:37

7 Answers7

4

For future reference, I've run into this problem when starting a project using MongoDB (via mLab). Remember that you first have first create a 'Database User' and those credentials go into your connection URL: mongodb://<dbuser>:<dbpassword>@ds11111.mlab.com:11111/your-db-name

The dbuser and the dbpassword are not your mLab login credentials, but the credentials when you make the database user. A nuance I often forget.

adnauseam
  • 627
  • 6
  • 14
4

If it was working for you earlier but not after a while, it could be because you connected to a different network and IP is different now.

Check the IP and credentials that you provided in mongodb cloud config !!! For me IP was the issue.

If you are connected to different networks at different times, you can set it as 0.0.0.0/0 which will allow you to essentially connect from anywhere.

Gaurav Joshi
  • 482
  • 3
  • 9
3

I had similar error. Try to use option useMongoClient in connect method.

{ useMongoClient: true }

Like this:

mongoose.connect('mongodb://localhost:27017/test', { useMongoClient: true });

also in my case I tried to create new deployment in mlab and use it. Previously when I had an error I used one that I tried already to connect to another app..

2

from your error message, I assume that you were connecting using mongodb from mlab.

I ever encountered that error, and that's because my password contains '@' character. So after changing/removing the '@' character'. I think '@' character is used by mlab to separate the connection string.

Hope that helps you or someone else.

2

For me it was just the firewall, after changing internet connection it worked!

0

If you still have this kind of error I'll suggest you to change the region of database. It's help me to solve this error.

 UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [ds113454.mlab.com:13454] on first connect [MongoNetworkError: connect ETIMEDOUT 34.240.245.222:13454]
    at Pool.<anonymous> (C:\Users\sisakhanyan\Desktop\server\node_modules\mongodb-core\lib\topologies\server.js:564:11)
    at Pool.emit (events.js:180:13)
    at Connection.<anonymous> (C:\Users\sisakhanyan\Desktop\server\node_modules\mongodb-core\lib\connection\pool.js:317:12)
    at Object.onceWrapper (events.js:272:13)
    at Connection.emit (events.js:180:13)
    at Socket.<anonymous> (C:\Users\sisakhanyan\Desktop\server\node_modules\mongodb-core\lib\connection\connection.js:246:50)
    at Object.onceWrapper (events.js:272:13)
    at Socket.emit (events.js:180:13)
    at emitErrorNT (internal/streams/destroy.js:64:8)
    at process._tickCallback (internal/process/next_tick.js:178:19)
  • 1
    Maybe example how to change it will help also. – onetwo12 Nov 21 '18 at 11:23
  • @onetwo12 I'm searching examples too. I know that this solves my issue because I recreate database with new host region. I think for now you can export your database, then create new database with another host region and import your old database. – Sergey Isakhanyan Nov 21 '18 at 12:35
0

@asNauseam's answer was the trick for me. Didn't know there was a difference between a db user and your user username. This Forum got it done for me: https://docs.mlab.com/connecting/

orpheus
  • 1,060
  • 2
  • 15
  • 23