0

I have some Http call to the API, which returns some data. The data is as follows:

{
"id": 7728806673365567000,
"title": "Status Unknown"
}

And now, the problem is with ID, as in database, Id is like: 7728806673365567677 So, valid id has 677 as last 3 digits, but data returned from the API contains 000, so:

Valid: 7728806673365567677

Invalid: 7728806673365567000

I'm wondering what is the problem with this. Maybe it can work if ID will be a string not number ?

Thanks for any advice.

EDIT: Now i see that data in backend is in fact a string : "7728806673365567000" But Http call changes this into Number ?

user3573535
  • 545
  • 2
  • 7
  • 29

4 Answers4

2

It has nothing to do with NativeScript. You may call it a limitation with JavaScript, you are exceededing the maximum safe integer limit supported by JavaScript. The result will be same if you try this in Browser environment too.

So as you already mentioned use String instead of Number, that should fix this issue.

Manoj
  • 21,753
  • 3
  • 20
  • 41
  • Hi, Thanks. But how to convert this to string to get valid number? Right now the response gives me NUMBER000 so if i will convert this to string i will get 'NUMBER000' right ? Or you suggesting to do this in backend first ? – user3573535 Jan 10 '19 at 08:28
  • Yes, you have to handle it in the backend. If you refer any REST APIs they will handle the IDs as string when they cross this limit. – Manoj Jan 10 '19 at 08:34
  • HI, I've chencked backend, thw ID is a string : " 7728806673365567677" but Http call changes this to 7728806673365567677... Any ideas? Thanks. – user3573535 Jan 10 '19 at 08:59
  • That must be something to do with your backend, Http call won't change anything. You may cross check the response with any REST client like POSTMAN, make sure it's returning string. – Manoj Jan 10 '19 at 09:05
2

base on

Number.MAX_SAFE_INTEGER

9007199254740991

var d=7728806673365567677 ;

undefined

d.toString()

"7728806673365567000"

typeof d

"number"

Number.isSafeInteger(7728806673365567677 )

false

Number.isSafeInteger(7728806673365567)

true

so

var data = {
"id": 7728806673365567677,
"title": "Status Unknown"
};

data.id;

"7728806673365567677"

and

var data = {
"id": "7728806673365567677",
"title": "Status Unknown"
};

data.id;

7728806673365567000

for example in php (click to try the code)

$data= array(
    'id' => 7728806673365567677,
    'title' => 'Status Unknown',
);

echo json_encode($data,JSON_NUMERIC_CHECK);

should solve this problem and return

{
"id": "7728806673365567677",
"title": "Status Unknown"
};

you need to have a package called 'php5-json' installed

Mohamed Elrashid
  • 8,125
  • 6
  • 31
  • 46
1

Yes, you should threat that as a string. It is larger then the max. possible number in JS 2^53 - 1, or 9007199254740991.

Because of that its truncated, the numbers taken away replaced with zeros. I dont know where this specific behaviour comes from, but that seems to be happening.

See: What is JavaScript's highest integer value that a number can go to without losing precision?

LevinFaber
  • 11
  • 1
  • 2
1

You can use, BigInteger NPM Module to solve this, also you can use some parsers for this problem, see this,

Stackoverflow Question

M. Çağlar TUFAN
  • 626
  • 1
  • 7
  • 21