Error using Mongoose [connect ECONNREFUSED 127.0.0.1:27017]

I’m trying to connect the databse (I’m new at this) and worked once but now won’t work again, some videos say that you have to start MongoDB service, but they do it from their Windows, how can I start it on glitch?

The manager module

const mongoose = require("mongoose");

const Mongo = {
  Con: async function() {
    await mongoose.connect("mongodb://localhost/Profiles", {
      useNewUrlParser: true,
      useUnifiedTopology: true
    });
    console.log("HERE")
    return mongoose;
  },
  Profile: mongoose.model(
    "Profile",
    mongoose.Schema({
      Id: String,
      Name: String,
      Lvl: Number,
      Coins: Number,
      XP: Number
    })
  )
};

module.exports = Mongo;

Test command to try it

const mongo = require("../mongo.js");

module.exports = {
  name: "test",
  async execute(Discord, client, message, args) {
    console.log("First")
    let con = await mongo.Con();
    try {
      let profile = new mongo.Profile({
        Id: message.author.id,
        Name: message.author.username,
        Lvl: 5,
        Coins: 100,
        XP: 400
      });
      console.log(profile);
      profile
        .save()
        .then(r => console.info(r + "R"))
        .catch(e => console.error(e + "E"));
    } finally {
      con.connection.close();
    }
  }
};

And the result on console is this one (Personalized errors)

First
MongooseServerSelectionError in test command: connect ECONNREFUSED 127.0.0.1:27017

Hello. It looks like you’re trying to Connect mongodb to Localhost.

Do you have mongodb server?
If no, go create one project then on your client, Target it to your Mongodb server URL.

1 Like

I don’t

And I don’t have any idea how to do that, do you have a link to a tutorial or can at least briefly explain it?

I don’t recommend setting up a mongo server on Glitch, instead, you should look into something like MongoDB atlas.

3 Likes

Yeah, it’s not recommended at Glitch.

You can try these database manager as your mongodb alternative:

You can go to https://mongodb.com

No, no offense, but don’t use these as MongoDB alternatives.


Once you sign up at atlas.mongodb.com, you can create a cluster on their free tier. Once you’ve done that, they’ll give you a url to connect to the cluster, which you can plug into mongoose.connect('that url here'); and data will get stored in that cluster.

I’d suggest searching for tutorials and videos about using Mongoose with MognoDB Atlas (I had found one really nice video, but now I can’t seem to find it).

5 Likes

If you already have a mongodb, you might have to whitelist 0.0.0.0 to allow connections from anywhere.

2 Likes

The reason that is: it’s because that Glitch Project’s IPs switches a few times

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.