Vicdenz
Vicdenz

Reputation: 27

connect ECONNREFUSED 127.0.0.1:3000 - DB Mongoose with Nodejs

I'm trying to get a simple database started but when I start my nodejs file, I keep getting this error message connect ECONNREFUSED 127.0.0.1:3000 from mongoose.connect(). I'm new to mongoose but I'm pretty sure my code is right. This is my code:

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:3000/cats_app', {
  useNewUrlParser: true,
  useUnifiedTopology: true
})
    .then(() => {
        console.log('Connected to DB!')
})
    .catch(err => {
       console.log("Couldn't console to DB:", err.message)
})

const catSchema = new mongoose.Schema({
    name: String,
    age: Number,
    mood: String
})

var Cat = mongoose.model("Cat", catSchema)

var george = new Cat({
    name: "George",
    age: 11,
    mood: "Grouchy"
})

george.save()
    .then(item => {
        console.log("saved:", item);
    })
    .catch(err => {
      console.log("error:", err.message);
    });

Upvotes: 0

Views: 874

Answers (1)

O. Jones
O. Jones

Reputation: 108766

MongoDB, by default, accepts connections on port 27017. You're trying to connect on port 3000.

Try this, changing the :3000 port specification in the MongoDB URL.

mongoose.connect('mongodb://localhost:27017/cats_app', {
  useNewUrlParser: true,
  useUnifiedTopology: true
})

Upvotes: 1

Related Questions