1

I am using Discord.JS v13 for sharding, and I am using mongoose for the database. I connect to mongoose in my sharding file (index.js) rather than in the bot.js because I need to use it there, but this isn't allowing me to get data from mongoose anywhere but index.js. I don't know why is this happening as this was perfectly fine a few days back and I haven't changed anything.

index.js (Sharding File)

// .....Sharding Manager
const dbURI = process.env.DBURI;

const mongoose = require("mongoose");
// noinspection JSCheckFunctionSignatures
mongoose.connect(dbURI, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});

/models/user.js (Schema File)

const mongoose = require("mongoose");
const userinfo = new mongoose.Schema({
  UserID: {
    type: String || Number,
    required: true,
  },
  /** Whole schema **/
});

const MessageModel = (module.exports = mongoose.model("muser_userinfo", userinfo));

scommands/filters.js (The File I want to use it at!)


const userinfo = require("../models/user.js");

const user_id = interaction.user.id;

const data = await userinfo.findOne({ UserID: user_id });
    if (!data) {
//....

Error

7|muser | MongooseError: Operation muser_userinfos.findOne()` buffering timed out after 10000ms 7|muser | at Timeout.<anonymous> (/root/Bots/muser/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:185:20) 7|muser | at listOnTimeout (node:internal/timers:559:17) 7|muser | at processTimers (node:internal/timers:502:7) 

I have tried everything you can possibly think of, used find() instead of findOne(), used .then() on connect, double-checked the credentials and what not!

bguiz
  • 27,371
  • 47
  • 154
  • 243
PGamerX
  • 23
  • 1
  • 5

2 Answers2

1

The ShardingManager generally spawns a process for each shard (unless you specify otherwise). If you're only connecting to your Mongo database from the sharding file then your bot client won't have access to it.

Try connecting to your Mongo database from the client too, it shouldn't matter too much since Mongo supports multiple connections.

moonstar-x
  • 163
  • 7
  • God, I was being so silly lol. Shards create a new instance for each shard but I wasn't exporting the existing mongoose object from the sharding manager to the main file! I just connected to Atlas once again in the main file and now it worked. Thank you – PGamerX Mar 28 '22 at 13:37
0

In my experience of using mongoose, its throwing an error because of low internet connection, but looking in other documents. this is what ive found that can help you

In my experience this happens when your database is not connected, Try checking out following things -

  • Is you database connected and you are pointing to the same url from your code.
  • check if your mongoose.connect(...) code is loading.

I faced this issue where I was running the node index.js from my terminal and mongoose connect code was into different file. After requiring that mongoose code in index.js it was working again. This is the source link --Biggest credit for @Abhishek Gangwar

新Acesyyy
  • 1,152
  • 1
  • 3
  • 22