NOTE: All dates are waffle, i’m just testing.
The options from the discord command are not being saved into the mongodb database. When looking at the database viewer, it seems that it’s not recognising that the new options aren’t a thing, sorry if i didn’t explain it to well.
Schema: gameInfos.js
const { model, Schema } = require('mongoose');
let gameInfos = new Schema({
Name: String,
ReleaseDate: String,
TimeToRelease: Number,
ImageURL: String,
Achievements: Number,
TimeToComplete: Number
})
module.exports = model("gameInfos", gameInfos)
View of data in database – ESSENTIAL
Main command
const { SlashCommandBuilder } = require("@discordjs/builders");
const chalk = require("chalk");
const gameInfos = require('../../schemas/gameInfos')
module.exports = {
data: new SlashCommandBuilder()
.setName("add-game-release")
.setDescription(
"Add a game to the game release database. All game releases will be sent in #new-games! ADMINS ONLY"
)
.addStringOption((option) =>
option
.setName("gamename")
.setDescription(
'Please provide the name of the game that you wish to use. For example: "Cyberpunk 2077"'
)
.setRequired(true)
)
.addIntegerOption((option) =>
option
.setName("releaseday")
.setDescription(
"Please provide the release day. EG: 21 in 21st of November 2024."
)
.setRequired(true)
)
.addStringOption((option) =>
option
.setName("releasemonth")
.setDescription(
"Please provide the release month. EG: November in 21st of November 2024"
)
.setRequired(true)
.addChoices(
{ name: "Janurary", value: "1" },
{ name: "February", value: "2" },
{ name: "March", value: "3" },
{ name: "April", value: "4" },
{ name: "May", value: "5" },
{ name: "June", value: "6" },
{ name: "July", value: "7" },
{ name: "August", value: "8" },
{ name: "September", value: "9" },
{ name: "October", value: "10" },
{ name: "November", value: "11" },
{ name: "December", value: "12" }
)
)
.addIntegerOption((option) =>
option
.setName("releaseyear")
.setDescription(
"Please provide the release year. EG 2024 in 21st November 2024"
)
.setRequired(true)
.setMinValue(4)
)
.addStringOption((option) =>
option
.setName("imageurl")
.setDescription(
"Please provide a link for the game cover. This is required."
)
.setRequired(true)
)
.addIntegerOption((option) =>
option
.setName("achievements")
.setDescription(
"Please provide the number of achivements. 0 if there are none."
)
.setRequired(true)
)
.addIntegerOption((option) =>
option
.setName("timetocomplete")
.setDescription("Please provide the time to complete/get platnium IN HOURS")
.setRequired(true)
),
async execute(interaction) {
const { options } = interaction;
let currentDate = new Date();
console.log(currentDate);
const gameTitle = options.getString("gamename");
const day = options.getInteger("releaseday");
const month = options.getString("releasemonth");
const year = options.getInteger("releaseyear");
const imageURL = options.getString("imageURL")
const achievements = options.getInteger("achievements")
const timetocomplete = options.getInteger("timetocomplete")
let releaseDate = `${month}/${day}/${year}`;
// To calculate the time difference of two dates
let Difference_In_Time =
new Date(releaseDate).getTime() - new Date(currentDate).getTime();
// To calculate the no. of days between two dates
let Difference_In_Days = Math.round(
Difference_In_Time / (1000 * 3600 * 24)
);
gameInfos.create({
Name: gameTitle,
ReleaseDate: releaseDate,
TimeToRelease: Difference_In_Days,
ImageURL: imageURL,
achievements: achievements,
timetocomplete: timetocomplete
});
console.log(timetocomplete)
interaction.reply("Sent to console.");
console.log(
`${chalk.bold.blue(
"[LOG]"
)} A game release reminder called ${gameTitle} was added to the database. The release date is ${day}/${month}/${year}. That's ${Difference_In_Days} days away from now.`
);
},
};
2
Answers
Your
Mongoose
Schema setup is fully correct but you have a couple of problems in your implementations. Let’s go togetherTypo in Schema Fields
According to your schema setup,
(Achievements, and TimeToComplete)
this too filled you did not use it correctly. You’re using lowercase versions(achievements and timetocomplete)
. MongoDB is case-sensitive, so these should match.But update your schema as follows:
Mismatch in Option Names
When retrieving options from the interaction, you’re using
options.getString("imageURL")
, however, the option is defined asimageurl
in your command builder. Verify that the names correspond. Also, ensure that you are using the correct casing for other option names in theexecute function
Update the line like this:
Data Types for Time Calculations
Although you are giving a string
Difference_In_Days
to theexecute function
, theTimeToRelease
field in the schema is defined as a number. To ensure similarity, turn it into a numberNote: Do check to understand yourself
Math.round(2.8)
always return type ofnumber
.@I hope your code will work if you do set it perfectly
use
await
inside async function where before creating data through mongoose model.