[英](node:16686) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
While conencting mongoDb with application facing issue: "UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017"在连接 mongoDb 时遇到应用程序问题:“UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017”
Tried Solutions:尝试过的解决方案:
Code:代码:
var express = require('express');
var app = new express();
var bodyParser = require('body-parser')
var mongoose = require('mongoose')
var user = require('./model/user')
const hostname = '127.0.0.1';
const port = 3000;
var router = express.Router();
mongoose.connect('mongodb://localhost:27017/userdb', { useNewUrlParser: true });
var users = [{ name: "Jane", age: 20 }, { name: "Mark", age: 40 }, { name: "Janefer", age: 40 }]
app.use(bodyParser.urlencoded({ extended: true }))//converts it into obj
router.route('/users')
.get((req, res) => {
//fetch record from users collectiobn
user.find({}, (err, users) => {
if (err) {
res.json({ msg: 'error in fetching data from DB' })
}
else {
res.json(users)
}
})
// res.json(users)
})
.post((req, res) => {
})
app.use('/api', router)//middleware to direct all req
app.listen(port, hostname, () => {
console.log("Server listening on 3000")
})
Error screenshot:错误截图:
you can try to check which port mongodb is using in your mongodb.conf after that if you still are have problem you can run the command ps aux | grep mongo
您可以尝试检查您的 mongodb.conf 中使用的端口 mongodb 之后,如果仍然有问题,您可以运行命令
ps aux | grep mongo
ps aux | grep mongo
and post the result. ps aux | grep mongo
并发布结果。
Instead of [localhost] use [127.0.0.1].而不是 [localhost] 使用 [127.0.0.1]。 like this - 'mongodb://127.0.0.1:27017/userdb'
像这样 - 'mongodb://127.0.0.1:27017/userdb'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.