#MongoDB issue

81 messages · Page 1 of 1 (latest)

steady sinew

as in the other post, you should check if data is existent before accessing properties from it

simple if statement

very basic

if user is null, then there is no data

fast cipher

this is btw a different issue I picked up on when i reset my database

I have removed the original thing for other reasons but this is mainkly what I am focusing on

fast cipher

it should be placing this data under users

const userSchema = new mongoose.Schema({
userId: { type: String, required: true },
xp: { type: Number, default: 0 },
level: { type: Number, default: 0 },
});

shouldn't new data be being created?

// Add 100 xp for every command
const userId = interaction.user.id;
const userreply = interaction.user;
const user = await User.findOneAndUpdate({ userId }, { $inc: { xp: 100 } }, { new: true });

// Check if the user has reached 1000 xp to level up
if (user.xp >= 1000) {
const newLevel = user.level + 1;
await User.findOneAndUpdate({ userId }, { xp: 0, level: newLevel }, { new: true });
await interaction.channel.send(Congratulations! ${userreply} You have reached level ${newLevel}.);

}

steady sinew

the issue is that you're not checking if (!user) return;

otherwise you wouldn't get that error

fast cipher

yeah Ik

but why would I need to do that anyways

if the bot creates that data?

the code should be making that data, or adding that data.

steady sinew

no, you would be making that check to see if the user is false or in this case, not returning any data

you get null reading xp because user is null which is the data from your dabatase

so you cant access user.xp if user is null

im basically repeating myself

fast cipher

adding if (!user) return; only stops an error from going through

so the bot can keep going

steady sinew

it wont

fast cipher

exactly

steady sinew

the only time it'll keep going is if user has data

fast cipher

exactly

I kno

steady sinew

which in this case, there isnt

fast cipher

my point is

it's supposed to add 100xp to the user per command, why won't it do this?

Also, before that even happens, should data be created

?

there should be a user

in the database

steady sinew

if you get null reading xp then user is null which means there is no data so you should create data if that is the case

fast cipher

that's where this should be working right?

// Create a new model for users
const User = mongoose.model('User', userSchema);
steady sinew

thats just where you export the model

fast cipher

ignore

that

// Create a new model for users
const User = mongoose.model('User', userSchema);

this should be creating this


// Create a new schema for Level and XP
const userSchema = new mongoose.Schema({
  userId: { type: String, required: true },
  xp: { type: Number, default: 0 },
  level: { type: Number, default: 0 },
});

which then corresponds to

const userId = interaction.user.id;
  const userreply = interaction.user;
  const user = await User.findOneAndUpdate({ userId }, { $inc: { xp: 100 } }, { new: true });

which isn't happening?

so what is the issue

steady sinew

im not going to repeat myself

fast cipher
steady sinew

if user is null then you should create data

fast cipher

Shouldn't that data though for the specific user already exist when executing any command before adding 100xp to that user?

steady sinew

the picture says it all though

fast cipher
steady sinew the picture says it all though
// Add 100 xp for every command
  const userId = interaction.user.id;
  const userreply = interaction.user;
  const user = await User.findOneAndUpdate({ userId }, { $inc: { xp: 100 } }, { new: true });
  if (!user) {
    const newUser = ({ userId }, { $inc: { xp: 100 } }, { new: true });
      await User.insertOne(newUser);
  }

like this?

steady sinew

try it

fast cipher

TypeError: User.insertOne is not a function

steady sinew

have you looked at the mongoose docs at all?

fast cipher

yeah

steady sinew

im not a mongodb expert, but i do know a little bit so thats why im just wondering

i dont see insertOne anywhere

you may be looking for updateOne

fast cipher

ok

steady sinew

i do need to take a step back since there are more mongodb experts that can probably give better explanations than i am

i wish you luck

fast cipher

ok

yeah tbh none of this helped at all just saying. Ever since I added the currency category thing for mongodb

it hasn't been working

I literally remember test this

and it added the data

to the user

so yeah

@steady sinew Hey sorry for the ping but I think I fixed the issue mate

I know you were trying to help I am quite experienced with this stuff however sometimes I really just don't realize the issue

for some reason my code had the following removed:

upsert: true

that's all

steady sinew

if i came off a bit harsh i apologize, but i totally forgot about that option

i hope you fixed it!

fast cipher

literally

that was the only thing

causing this issue

I also realized that because the cooldown also contains this as well

steady sinew

lol that's why im better with plain djs or js than working with databases

ah okay well im glad you found the issue!

fast cipher

yeah I must have accidentally removed it

while trying to do something else

anyways thanks for trying to help though

appreciate