#MongoDB issue
81 messages · Page 1 of 1 (latest)
simple if statement
very basic
if user is null, then there is no data
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
the problem I have with what your saying here is that, Initially when I used this it definitely worked. So idk, something is wrong with the code.
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}.);
}
the issue is that you're not checking if (!user) return;
otherwise you wouldn't get that error
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.
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
adding if (!user) return; only stops an error from going through
so the bot can keep going
it wont
exactly
the only time it'll keep going is if user has data
exactly
I kno
which in this case, there isnt
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
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
that's where this should be working right?
// Create a new model for users
const User = mongoose.model('User', userSchema);
thats just where you export the model
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
im not going to repeat myself
if user is null then you should create data
Shouldn't that data though for the specific user already exist when executing any command before adding 100xp to that user?
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?
try it
TypeError: User.insertOne is not a function
have you looked at the mongoose docs at all?
yeah
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
ok
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
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
if i came off a bit harsh i apologize, but i totally forgot about that option
i hope you fixed it!
literally
that was the only thing
causing this issue
I also realized that because the cooldown also contains this as well
lol that's why im better with plain djs or js than working with databases
ah okay well im glad you found the issue!
yeah I must have accidentally removed it
while trying to do something else
anyways thanks for trying to help though
appreciate