#Help me please
44 messages · Page 1 of 1 (latest)
- Consider reading #how-to-get-help to improve your question!
- Explain what exactly your issue is.
- Post the full error stack trace, not just the top part!
- Show your code!
- Issue solved? Press the button!
This is the schema folder
const { Schema, model } = require("mongoose");
const userset = new Schema({
userID: { type: String },
economia: {
banco: { type: Number, default: 0 },
cupcake: { type: Number, default: 0 },
},
});
module.exports = model("Usuários", userset);```
Show your code please
index
client.userdb = require("./schema/user.js")
mg.connect(process.env.mongodb, {
}).then(() => console.log(`Database logada com sucesso.`));```
Where the error is located.
Oh sorry, missed the first part
Code
const Discord = require("discord.js");
module.exports = {
name: "trabalhar",
description: "「Economia」trabalhe para receber uma recompensa.",
type: Discord.ApplicationCommandType.ChatInput,
run: async (client, interaction) => {
const userdb = await client.userdb.findOne({
userID: interaction.user.id
})
const emprego = [
"Pedreiro",
"Pintor",
"Bombeiro",
"Policial",
"Animador de Restaurante",
"Feirante",
"Traficante",
"Ladrão de carros",
"Presidente",
"Vendedor De Carros"
]
let cupcake = Math.floor(Math.random() * 2000) + 200
await client.userdb.updateOne({
userID: interaction.user.id
}, { $set: {
"economia.cupcake": userdb.economia.cupcake + cupcake }
})
const emprego1 = emprego[Math.floor(Math.random() * emprego.length)]
const embed = new Discord.EmbedBuilder()
.setTitle(`( ✅ ) Trabalho realizado`)
.setColor("#8D7866")
.setDescription(`** ✅ - **<@${interaction.user.id}> você trabalhou de **${emprego1}** e faturou **${dinheiro} Cupcakes**.`)
interaction.reply({embeds: [embed]})
}
}```
.
Correct me if I'm wrong, I haven't used mongodb in a long time. But where you have userdb.economia.cupcake + cupcake, userdb.economie.cupcake isn't the location of the #? Wouldn't it be within the findOne's ID?
I am going to bed, good luck
findOne (line 1) is returning undefined
so you're setting userdb = undefined and then trying to read (undefined).economia, which doesn't exist
either the query is incorrect or there's no db entry for that user
that's what i want to know
I've been asking for help for days to store in the database and no one helps me
I was told to put this in index.js and I put
@daring stirrup can you help me?
ok so basically, findOne will return undefined if the user is not already in the database
so your code should check if the result is undefined, and substitute in a default value
But is he right? why doesn't anyone explain to me the right way to do it then I don't know how it is
@daring stirrup help me to solve this problem
I have no clue what eX meant in their message
I haven't used mongodb before, but logically findOne should return whatever object matches the query (or undefined if no such object)
findOne() returns null specifically if there is no data. You can use some sorta tenary operator for example
userdb ? userdb.economia.cupcake + cupcake : cupcake
@loud quail this is for you
Can you help me with mongodb, I've been asking for help for days but nobody helps me, I need help saving information and setting money, reputation etc
But no one helps me
Sure just specify what you need help with
first is this right?
Is this correct?
This is right depending on what your desired outcome is
But to me from what I can gather
Yes
Yes I think so
Okay, as I am making a command work, it needs to save the user information in the money case.
When I go to use the wallet, the bot will find his information in the case of money and money in the bank.
What you’re doing is correct
Just if the db doesn’t find any document witg that userId it’ll return null
So you’ll either wanna make a document where you findOne() add the money to the user
Or you’ll want to use what I said and then also add upsert: true into the third part of the options in updateOne()