Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoError: failed to connect to server on first connect

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:[email protected]: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' }

like image 586
Aryan Avatar asked Dec 09 '16 12:12

Aryan


2 Answers

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.

like image 190
adnauseam Avatar answered Nov 15 '22 06:11

adnauseam


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.

like image 44
Gaurav Joshi Avatar answered Nov 15 '22 06:11

Gaurav Joshi