Home > Software engineering >  (discord.js v14) MongoDB is not saving users discord ids correct
(discord.js v14) MongoDB is not saving users discord ids correct

Time:11-04

I save users' data by their discord IDs to my database and until today I only used it to fetch their data but I tried to fetch users by their ID saved to the database and saw it cannot find the user because the ID in the database is incorrect.

For example, if users id 358262932791885824 it saves it like this 358262932791885800 or if users id 243422653547937792 it saves it like this 243422653547937800. I was searching for a solution and found out someone in this post saying it's because of Number.MAX_SAFE_INTEGER but I didn't understand it and couldn't find a solution for the problem. Would be very happy if you help me understand the situation and solve the problem!

If the user doesn't have data saved on his discord ID this code creates a new one

        let kontrol = await user.findOne({discord_id: message.author.id});
        if (!kontrol) {
            let newUser = new user({
                discord_id: message.author.id,
                xp: 0,
                level: 0,
                money: 0,
                inventory: [],
                daily: {
                    last: 0,
                    next: 0
                },
                warnings: {
                    adWarn: 0
                }
            })
            await newUser.save();
        }

My user schema

const mongoose = require('mongoose')
const user = new mongoose.Schema({
    discord_id: {
        type: Number,
        unique: true,
        required: true
    },

    xp: {
        type: Number,
        default: 0,
        required: true
    },

    level: {
        type: Number,
        default: 0,
        required: true
    },

    money: {
        type: Number,
        default: 0,
        required: true
    },

    inventory: {
        type: Array,
        default: [],
        required: true
    },

    daily: {
        last: {
            type: Number,
            default: 0,
            required: true
        },
        next: {
            type: Number,
            default: 0,
            required: true
        }
    },

    warnings: {
        adWarn: {
            type: Number,
            default: 0,
            required: true
        },
    }
});

module.exports = mongoose.model('user', user)
```js


Tried to save users datas with discord ids.
Data saved without error or problem but the discord id is not correct.
When i try to fetch data by id tho, theres no problem fetching it from database.
But when i try to fetch user by id from database, i cant.

CodePudding user response:

It means discord_id should be a string. Every Discord ID/snowflake should be stored as a string. If you store an ID as an integer and it's larger than Number.MAX_SAFE_INTEGER (2^53) it will not be representable in the number type and will be converted to something else by JavaScript (like changing the last to digits to zeroes).

discord_id: {
  type: String,
  // ...
}

Also see Why is my query output showing something completely different than what it is in the database? and Modified data when using sqlite3

  • Related