在nodeJS中处理从数据库返回的大数字的最佳方法是什么? [英] What is the best way to handle big number returning from database in nodeJS?

查看:37
本文介绍了在nodeJS中处理从数据库返回的大数字的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从 MySql 数据库获取数据的节点 JS 应用程序.一列的列类型是 bigint(20).这将返回一个大整数值,这超出了 javascript 数字范围.处理此问题的最佳方法是什么?

I have a node JS application getting data from MySql Database. Column type of one the column is bigint(20). This will return a big integer value and this is going out of javascript number range. What is the best way to handle this?

示例代码

let rows = database.query("select resourcekey from resourceTable");

for(var i = 0; i < rows.length; i++) {
  console.log(rows[i].resourcekey);
}

示例资源密钥:9220610450398789367返回值:9220610450398790000

Sample resourceKey : 9220610450398789367 Value returned : 9220610450398790000

推荐答案

NodeJS mysql 提供了 supportBigNumbers 和 bigNumberStrings 配置选项.启用这两者将强制返回大数字(BIGINT 和 Decimal)以作为 javascript 字符串返回.

NodeJS mysql provides supportBigNumbers and bigNumberStrings configuration options. Enabling both of this will force to return big numbers(BIGINT and Decimal) to be returned as javascript string.

let dbConnection = mysql.createConnection({supportBigNumbers: true, bigNumberStrings: true});

这篇关于在nodeJS中处理从数据库返回的大数字的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆