0

I work with Discord.js User object and Mongoose Schema. But the problem doesn't seems to be part of those.

var Message = require('../app/models/message'); //Mongoose Schema
  ...
var newMessage = new Message();
  ...
//taggedUser is an object containing all the info about user. id property contains user id which is number.
const taggedUser = message.mentions.users.first(); 

newMessage.message.to       = taggedUser.id;
console.log(taggedUser.id);
console.log(newMessage.message.to);

The code above should assign user ID to Schema. Everything works, but...

442090269928849410

442090269928849400

Last 2 characters aren't the same among these variables now. How is this even possible? The = changed the actual data inside the variable?

In case it is Mongoose here is how Schema looks like:

var msgSchema = mongoose.Schema({

    message          : {
        from         : Number,
        to           : Number,
        content      : String,
        time         : Date
    }

});

Edit:

If I change

to           : Number,

to string:

to           : String,

It works properly. I still need the answer on why does this work incorrectly with number. Right above the problematic line I have another id which works perfectly fine:

newMessage.message.from     = msg.author.id;

I have already tried to parse taggedUser.id to integer or creating Number() object but that didn't help. So every time I turn taggedUser.id into a Number or parse it to int it changes to the slightly different number.


I don't know what to think. How can data change during the assignment?

If there is not enough data provided in the question please ask me and I'll add everything needed. I can't imagine what might be causing this bug.

Telion
  • 727
  • 2
  • 10
  • 22
  • @Neil Lunn I am not retrieving any data from MongoDB. I am creating a new object. The "duplicate" is not related to my question at all. – Telion May 06 '18 at 05:01
  • Show the surrounding code where you get the object from and making the assignment then. Your question currently gives no indication of where `newMessage` even comes from. Don't blame us for calling out when it "looks like a duck". It's up to you to show a clear example. – Neil Lunn May 06 '18 at 05:04
  • @Neil Lunn noone asked me to provide more code, it was just aggressively marked as a duplicate. I updated the question, there is nothing else in the code that anyhow may affect the process. – Telion May 06 '18 at 05:13
  • So you don't actually assign `newMessage.message` anywhere right? And the "only" code goes straight to `newMessage.message.to = `. Because that's what it's looking like here. Also tone down your language. Nobody is "aggressively" doing anything. – Neil Lunn May 06 '18 at 05:22
  • @Neil Lunn yes I am not assigning it anywhere except where you can see. So this is really weird. How can only 1 symbol change and why? – Telion May 06 '18 at 14:51

2 Answers2

2

9007199254740992 - Highest safe number in JS

442090269928849410 - Your integer (id)

The reason of that small variation is the 'Max precision' JavaScript can work with.

When you tried to use the id as a number it was affected by this and it changed because JavaScript can't be that precise.

If you see both numbers at the beginning of this answer you can see that they are separated by 2 characters, that is why only the 2 last character changed.

Basically your integer was affected by the max precision JS numbers can have.

Reference: https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Number/MAX_SAFE_INTEGER

Community
  • 1
  • 1
Rodrigo Soriano
  • 119
  • 1
  • 1
  • 8
  • Oh... I never thought about that. Thanks so much! So I should use string to store these IDs? How does Discord store them? (theoretically) – Telion May 07 '18 at 01:35
  • Yes, discord uses ids to recognize users: For example if you are trying to register a new user with the `id = '123'` Discord will save that user like this: `'123': username:'abc' etc...(json format)` And basically you cannot use numbers as names for your json fields, that's why you use strings instead! (And the precision is a problem too!) – Rodrigo Soriano May 07 '18 at 01:44
0

You might just be seeing an artifact of console.log running asynchronously. Try this:

console.log('' + taggedUser.id);
console.log('' + newMessage.message.to);

...and see if that makes any difference.

kshetline
  • 12,547
  • 4
  • 37
  • 73