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' }
答案 0 :(得分:2)
从您的错误消息中,我假设您使用mlab中的mongodb进行连接。
我遇到过这个错误,那是因为我的密码包含' @ '字符。更改/删除' @ '字符后'。我认为mlab使用' @ '字符来分隔连接字符串。
希望能帮到你或其他人。
答案 1 :(得分:2)
对我来说,它只是防火墙,更改了Internet连接后就可以了!
答案 2 :(得分:2)
如果它对您较早但不对您有用,则可能是因为您连接到另一个网络并且IP现在不同了。
检查您在mongodb云配置中提供的 IP 和凭据!对我来说,知识产权是问题。
如果您在不同时间连接到不同的网络,则可以将其设置为0.0.0.0/0,这实际上使您可以从任何地方进行连接。
答案 3 :(得分:1)
为了将来参考,我经常在使用MongoDB(通过mLab)启动项目时遇到此问题。请记住,您首先要创建一个数据库用户&#39;这些凭据会进入您的连接网址:mongodb://<dbuser>:<dbpassword>@ds11111.mlab.com:11111/your-db-name
dbuser和dbpassword不是您的mLab登录凭据,而是您创建数据库用户时的凭据。我经常忘记的细微差别。
答案 4 :(得分:1)
我有类似的错误。尝试在连接方法中使用选项useMongoClient。 {useMongoClient:true}
像这样: mongoose.connect('mongodb:// localhost:27017 / test',{useMongoClient:true});
在我的情况下,我也尝试在mlab中创建新部署并使用它。以前,当我遇到错误时,我使用了已经尝试连接到另一个应用程序的错误。
答案 5 :(得分:0)
如果仍然存在此类错误,建议您更改数据库区域。这可以帮助我解决此错误。
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)
答案 6 :(得分:0)
@asNauseam的回答对我来说是个窍门。不知道db用户和您的用户名之间没有区别。这个论坛为我完成了任务:https://docs.mlab.com/connecting/
答案 7 :(得分:0)
对我来说,mLab选项卡未在谷歌浏览器中打开..所以当我使用用户名和密码登录时。.我回到服务器并启动它,并成功了
奇怪的是,我经常发生..是否应始终打开mLab以便服务器运行mongodb?
任何方式尝试仅登录您的mLab帐户,看看是否解决了 它