简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM