4

I have a Bigint column in my Table in SQL server as a Primary Key and when I request from a client with axios or fetch in React, it has a problem with ids like 9223372036854775800 and converts all of them to 9223372036854776000 !!!,

How can I fix that?

oguz ismail
  • 1
  • 16
  • 47
  • 69
wakiwiki
  • 231
  • 4
  • 13
  • 2
    you should not use it as number, use string. and it's not related to reactjs. it's javascript issue. – Ehsan May 05 '19 at 17:13
  • 1
    Possible duplicate of [What is JavaScript's highest integer value that a number can go to without losing precision?](https://stackoverflow.com/questions/307179/what-is-javascripts-highest-integer-value-that-a-number-can-go-to-without-losin) – adiga May 05 '19 at 17:33
  • Send a string from the server and use [**`BigInt`**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) – adiga May 05 '19 at 17:34
  • @adiga how can I use BigInt in react? – wakiwiki May 05 '19 at 18:52
  • Click the link the in the comment. – adiga May 05 '19 at 19:08

3 Answers3

4

To enable the BigInt in React as of now you have to add this comment to your code:

/* global BigInt */

Please, refer to this.

mdmundo
  • 1,988
  • 2
  • 23
  • 37
1

For resolve this problem you can send on the front-end string instead of number. Native js not support big number. If you want work on front-end with big number, you can use bignumber js library: https://github.com/MikeMcl/bignumber.js/

In your case: 1. send string on front-end. 2. get number as string and create BigNumber('9223372036854775800')

Timofey Goncharov
  • 1,023
  • 1
  • 6
  • 14
  • 1
    Thank you, but this package in React makes an array of digits of number and not return number, but with https://www.npmjs.com/package/big-integer it is ok. – wakiwiki May 05 '19 at 19:18
0

In your MySQL connection config, give a property

supportBigNumbers: true,