Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

mongodb - 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' }

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...