#How to fetch all data from MongoDB database document

73 messages Β· Page 1 of 1 (latest)

pliant remnant
#

I am creating a discord bot and I want to fetch all the data from the document, how would I do so?

#

For example:

#

_id
6541db76eb05fd997758dafd
Guild
"1165337640288145549"
Category
"1169138910589370368"
Handlers
"1167654090189324348"
Logging
"1169138911705055272"
__v
0
#

I want to get all this data

#

But I don't want it as a JSON

#

Because I'm using discord.js

fallen socket
pliant remnant
#

Okay thank you

pliant remnant
#

Unexpected token _ in JSON at position 4

pliant remnant
#

@fallen socket

fallen socket
pliant remnant
#

But when I try to do

Setup.find({ Guild: interaction.guild.id }).then((result) => { console.log(result) });
#

It returns as a JSON

fallen socket
#

What do you want it to be?

pliant remnant
#

I want to fetch the data, for example Handlers is a role ID;
Then I make a variable with that role id that I fetched from the document and then use it in the message;
Example:

const HandlerRole = // Get the "Handlers" value from the DB
interaction.reply({ content: `Current Handler Role: <@${HandlerRole}>` });
#

@fallen socket

fallen socket
pliant remnant
#

Yea of that Guild

fallen socket
#

But the guild in discord is the whole server so every member right?

#

or is guild a key that you added for your bot to track? (is guild being used in your app for something other than what Discord calls a guild?)

pliant remnant
fallen socket
pliant remnant
#

So if I do result.Handlers it'll return the role Id only? And if I do result.Logging it'll return the logging channel?

pliant remnant
#
    Setup.find({ Guild: interaction.guild.id }).then((result) => {
      const HandlerRole = result.Handlers;

      console.log(HandlerRole);
    });
fallen socket
#

oh weird. are you available to come into one of the Voice chats and share screen?

pliant remnant
#

Yes but I won't be able to talk

fallen socket
pliant remnant
#

Ok thank you

fallen socket
#

No problem πŸ‘

pliant remnant
#

How would I make it so it's not inside of a .then

#

Because I can't use that variable outside of the .then

#

@fallen socket

#
const {
  SlashCommandBuilder,
  ChatInputCommandInteraction,
  EmbedBuilder,
  Client,
  PermissionFlagsBits,
  ButtonBuilder,
} = require("discord.js");

// ! Schemas !
const Setup = require("../../Base/Schemas/SetupData");

module.exports = {
  data: new SlashCommandBuilder()
    .setName("panel")
    .setDescription("Enable, Disable, or Configure bot settings")
    .setDMPermission(false)
    .setDefaultMemberPermissions(PermissionFlagsBits.Administrator),
  /**
   *
   * @param {ChatInputCommandInteraction} interaction
   * @param {Client} client
   */
  async execute(interaction, client) {
    const { guild } = interaction;

    // ! Getting Data !
    // const modmailData = await Setup.findOne({
    //   Guild: interaction.guild.id,
    // });
    Setup.find({ Guild: interaction.guild.id }).then((result) => {
      const HandlerRole = result[0].Handlers;
    });

    const mainPanel = new EmbedBuilder()
      .setTitle("System Panel")
      .setDescription(`${HandlerRole}`)
      .setColor("Blue");

    // ! Buttons !
    const modmailButton = new ButtonBuilder();
  },
};
fallen socket
#

either that or ...

pliant remnant
#

My brain is not braining

fallen socket
#

πŸ˜† happens to all of us, take a five minute break and a walk

pliant remnant
#

I don't understand what you said

fallen socket
#
const HandlerRole = Setup.find({ Guild: interaction.guild.id }).then((result) => {
  return result.Handlers;
});
#

πŸ‘† thats one way to do it

pliant remnant
#

Ok let me try it

fallen socket
#

the other way is

let HandlerRole
Setup.find({ Guild: interaction.guild.id }).then((result) => {
  HandlerRole = result[0].Handlers;
});
#

might need some adjusting if i messed up the syntax but either one of those should work

pliant remnant
#
    const HandlerRole = Setup.find({ Guild: interaction.guild.id }).then(
      (result) => {
        return result[0].Handlers;
      }
    );

    const mainPanel = new EmbedBuilder()
      .setTitle("System Panel")
      .setDescription(`${HandlerRole}`)
      .setColor("Blue");

Returns: [object Promise]

fallen socket
#

oh true its returning the promise... the second way should work?

pliant remnant
#

Returns as undefined

#

Actually wait one sec

fallen socket
#

it will take a second

#

because it will start as undefined until it gets the result from Mongo

pliant remnant
#

If I console.log it it turns the ID in the console

#

But in the Embed it's undefined

#
    let HandlerRole;
    Setup.find({ Guild: interaction.guild.id }).then((result) => {
      HandlerRole = result[0].Handlers;
      console.log(HandlerRole);
    });

    const mainPanel = new EmbedBuilder()
      .setTitle("System Panel")
      .setDescription(`<@${HandlerRole}>`)
      .setColor("Blue");
fallen socket
#

thats because EmbedBuilder is being run while you are still waiting on the result of the promise.

pliant remnant
#

So how would I fix that?

fallen socket
#

good question πŸ˜†

#

you might have to move it inside that then or call it from inside the then

pliant remnant
#

Ok I put it inside the .then now it works

#

Thanks a lot

#

So I can't put anything after the find?

fallen socket
#

if anything depends on the values inside the promise you will have to find a way to get them. There might be a better way to structure it, but I don't know how at the moment.

pliant remnant
#

What if I don't have any data?

#
    Setup.find({ Guild: interaction.guild.id }).then((result) => {
      const mainPanel = new EmbedBuilder();
      if (!result) {
        mainPanel.setDescription("No Data was found");
        HandlerRole = "No Data was found";
      } else {
        HandlerRole = result[0].Handlers;
        mainPanel
          .setTitle("System Panel")
          .setDescription(`<@&${HandlerRole}>`)
          .setColor("Blue");
      }
      console.log(HandlerRole);

      interaction.reply({ embeds: [mainPanel], components: [menu] });
    });
#

This doesn't work

#
TypeError: Cannot read properties of undefined (reading 'Handlers')
    at C:\Users\1dxy\Desktop\RevoMail\Commands\Utilities\Panel.js:50:33
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
#

Line 50 is ```js
HandlerRole = result[0].Handlers;

#

@fallen socket

#

I'm going to go for now, let mem know how I can fix this ^

pliant remnant
#

Fixed it blobparty

fallen socket
#

πŸ’ͺ Let's go!