#Help me please

44 messages · Page 1 of 1 (latest)

loud quail
const userdb = await client.userdb.findOne({
userID: interaction.user.id
})
let cupcake = Math.floor(Math.random() * 2000) + 200

await client.userdb.updateOne({
         userID: interaction.user.id
     }, { $set: {
         "economia.cupcake": userdb.economia.cupcake + cupcake }
})```
steep roverBOT
  • 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!
loud quail

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);```
earnest hinge

Show your code please

loud quail

index

client.userdb = require("./schema/user.js")
           
mg.connect(process.env.mongodb, {
}).then(() => console.log(`Database logada com sucesso.`));```
earnest hinge

Where the error is located.

Oh sorry, missed the first part

loud quail

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]})    
}
}```
earnest hinge

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

daring stirrup

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

loud quail

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

loud quail

@daring stirrup can you help me?

daring stirrup

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

loud quail
loud quail

@daring stirrup help me to solve this problem

daring stirrup

I haven't used mongodb before, but logically findOne should return whatever object matches the query (or undefined if no such object)

lucid idol

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

loud quail

But no one helps me

lucid idol

Sure just specify what you need help with

lucid idol

But to me from what I can gather

Yes

loud quail

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.

lucid idol

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()