#How to fetch all data from MongoDB database document
73 messages Β· Page 1 of 1 (latest)
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
You have to receive the data somehow, so Mongo will send it as a string using JSON. Once you have the JSON data you can parse and store it as an object you can use in your actual code. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
Okay thank you
I get an error
Unexpected token _ in JSON at position 4
I think it's talking about _id or __v
@fallen socket
Oh, I think Mongo actually does returns the data as an object. You don't need json parse. You should be able to access whatever you get from Mongo directly in your code or through the Discord.js helper functions.
But when I try to do
Setup.find({ Guild: interaction.guild.id }).then((result) => { console.log(result) });
It returns as a JSON
What do you want it to be?
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
So this looks like its targeting the whole guild so it will return an array of objects right?
Yea of that Guild
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?)
Yes
The Guild is the guild id, the category is a category id, the handlers is a helper role id, the logging is a channel id
would it not just be const HandlerRole = result.handlers; ?
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?
I tried console.logging the HandlerRole, it returns as undefined
Setup.find({ Guild: interaction.guild.id }).then((result) => {
const HandlerRole = result.Handlers;
console.log(HandlerRole);
});
oh weird. are you available to come into one of the Voice chats and share screen?
Yes but I won't be able to talk
hmm that might be fine I'm in #735946723016442008
Ok thank you
No problem π
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();
},
};
You are declaring HandlerRole as a const inside your then. but you can declare it as a let outside of the then and then update it inside the then
either that or ...
My brain is not braining
π happens to all of us, take a five minute break and a walk
I don't understand what you said
const HandlerRole = Setup.find({ Guild: interaction.guild.id }).then((result) => {
return result.Handlers;
});
π thats one way to do it
Ok let me try it
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
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]
oh true its returning the promise... the second way should work?
it will take a second
because it will start as undefined until it gets the result from Mongo
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");
thats because EmbedBuilder is being run while you are still waiting on the result of the promise.
So how would I fix that?
good question π
you might have to move it inside that then or call it from inside the then
Ok I put it inside the .then now it works
Thanks a lot
So I can't put anything after the find?
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.
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 ^
Fixed it 
πͺ Let's go!