Search code examples
mysqlsqlnode.jsphpmyadminnode-mysql

Node.js rounds my sql result. how do i prevent this?


I am requesting a bigint from my database. However, Node.js alters the value of my request, so that my program doesn't work.

Here's my code:

let query = `SELECT transcriptchannel FROM guilds WHERE guild_id = 933323734322913301;`
    sql.query(query, function(err, result){
        if(err) throw err;
        
        console.log(result);
}

The console logs:

[ RowDataPacket { transcriptchannel: 946134226196123600 } ]

However, if i run the same statement in PHPmyAdmin, it looks like the following:

SELECT transcriptchannel FROM guilds WHERE guild_id = 933323734322913301;

it returns:

946134226196123658

Why does Node.JS round the value up and how do i prevent it?


Solution

  • This happens when the number is greater than Number.MAX_SAFE_INTEGER.

    Please try following and see if you are getting expected value:

    const sql = mysql.createConnection({supportBigNumbers: true, bigNumberStrings: true});
    

    reference - https://github.com/mysqljs/mysql