简体   繁体   English

(节点:16686)UnhandledPromiseRejectionWarning:MongooseServerSelectionError:连接 ECONNREFUSED 127.0.0.1:27017

[英](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:尝试过的解决方案:

  1. Reinstalling mongoDb重装 mongoDb
  2. Restarting the mongoDb service from control panel services.从控制面板服务重新启动 mongoDb 服务。
  3. Reinstalling the mongoose.重新安装 mongoose。

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:错误截图:

enter image description here在此处输入图像描述

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.

相关问题 MongooseServerSelectionError:连接 ECONNREFUSED 127.0.0.1:27017 - MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017 MongooseServerSelectionError:连接 ECONNREFUSED::1:27017 - MongooseServerSelectionError: connect ECONNREFUSED ::1:27017 在 Docker Compose 中获取“MongooseServerSelectionError:连接 ECONNREFUSED 127.0.0.1:27017” - Getting "MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017" in Docker Compose MongooseServerSelectionError:连接 ECONNREFUSED 127.0.0.1:27017。 我已经尝试过 StackOverflow 中给出的所有解决方案,但它不起作用 - MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017. I have tried all solutions given in the StackOverflow, but its not working MongooseServerSelectionError:连接 ECONNREFUSED::1:27017 不会得到修复 - MongooseServerSelectionError: connect ECONNREFUSED ::1:27017 wont get fixed (节点:25642)未处理的PromiseRejectionWarning:错误:ECONNREFUSED 连接ECONNREFUSED 127.0.0.1:50218 - (node:25642) UnhandledPromiseRejectionWarning: Error: ECONNREFUSED connect ECONNREFUSED 127.0.0.1:50218 节点app.js返回错误:连接ECONNREFUSED 127.0.0.1:27017 - Node app.js returns Error: connect ECONNREFUSED 127.0.0.1:27017 MongooseServerSelectionError:连接 ETIMEDOUT 13.250.154.115:27017 - MongooseServerSelectionError: connect ETIMEDOUT 13.250.154.115:27017 在节点 js 中连接 ECONNREFUSED 127.0.0.1:21 错误 - connect ECONNREFUSED 127.0.0.1:21 error in node js UnhandledPromiseRejectionWarning: MongooseServerSelectionError - UnhandledPromiseRejectionWarning: MongooseServerSelectionError
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM