ECONNREFUSED error when connecting to mongodb from node.js

前端 未结 13 1135
不思量自难忘°
不思量自难忘° 2020-11-27 05:51

I know I\'m doing some very stupid and noobish, but I\'m hoping someone can help me set up a basic database connection to mongodb from node.js on a mac.

I\'ve instal

相关标签:
13条回答
  • 2020-11-27 06:28

    I had facing the same issue while writing a simple rest api using node.js eventually found out it was due to wifi blockage and security reason . try once connecting it using your mobile hotspot . if this be the reason it will get resolved immediately.

    0 讨论(0)
  • 2020-11-27 06:30

    you can go to mongoDB compass client and follow these steps: 1.Click Fill in connection fields individually:

    2.In hostname type : 127.0.0.1

    3. Click CONNECT.

    0 讨论(0)
  • OK, this was another case of not being truly forthcoming in the info I posted above. My node.js app was very simple, but I was including another couple lines in my node.js code that apparently caused this issue.

    Specifically, I had another variable declared which was calling some other code that made a separate database call using incorrect db info. This is why, when using Xinzz's code, the console log error seemed not to change. It wasn't actually the mongoose.connect command that was throwing the error!

    Lesson learned, localize the problem and comment out unrelated code! Sorry guys, I knew this was me being dumb.

    0 讨论(0)
  • 2020-11-27 06:33

    I had same problem. It was resolved by running same code in Administrator Console.

    0 讨论(0)
  • 2020-11-27 06:35

    Use this code to setup your mongodb connection:

    var mongoose = require('mongoose');
    
    var mongoURI = "mongodb://localhost:27017/test";
    var MongoDB = mongoose.connect(mongoURI).connection;
    MongoDB.on('error', function(err) { console.log(err.message); });
    MongoDB.once('open', function() {
      console.log("mongodb connection open");
    });
    

    Make sure mongod is running while you start the server. Are you using Express or just a simple node.js server? What is the error message you get with the above code?

    0 讨论(0)
  • 2020-11-27 06:35

    I tried every possible solution,butit didn't help me. I took a break and I changed the following. Simple typo. May help someone is same situation. From: app.listen((port)=>console.log(Server is running at port ${PORT}))

    To: app.listen(PORT, console.log(Server is running at port ${PORT})) The earlier got me to connect to database mongo atlas but get request was Error: connect ECONNREFUSED

    0 讨论(0)
提交回复
热议问题