MongoError:首次连接时无法连接到服务器 [英] 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: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:首次连接时无法连接到服务器[ds047955.mongolab.com:47955]
MongoError: failed to connect to server [ds047955.mongolab.com:47955] on first connect
这是完整的日志:
内部{MongoError:首次连接时无法连接到服务器[ds127938.mlab.com:27938] 在游泳池. (/media/aryan/Adani/zellwk/node_modules/mongodb-core/lib/topologies/server.js:309:35) 在emitOne上(events.js:96:13) 在Pool.emit(events.js:188:7) 在连接. (/media/aryan/Adani/zellwk/node_modules/mongodb-core/lib/connection/pool.js:270:12) 在Connection.g(events.js:292:16) 在emitTwo(events.js:106:13) 在Connection.emit(events.js:191:7) 在套接字. (/media/aryan/Adani/zellwk/node_modules/mongodb- core/lib/connection/connection.js:185:10) 在Socket.g(events.js:292:16) 在emitNone(events.js:86:13) 名称:"MongoError", 消息:在首次连接时无法连接到服务器[ds127938.mlab.com:27938]"}
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' }
推荐答案
供以后参考,在使用MongoDB(通过mLab)启动项目时,我遇到了这个问题.请记住,您首先要先创建一个数据库用户",然后将这些凭据放入您的连接URL:mongodb://<dbuser>:<dbpassword>@ds11111.mlab.com:11111/your-db-name
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
dbuser和dbpassword不是您的mLab登录凭据,而是您成为数据库用户时的凭据.我经常忘记的细微差别.
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.
这篇关于MongoError:首次连接时无法连接到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!