2

I've got an API which I make a get request to fetch data. When i try to save the Id, I see Javascript round the last digit of it and it makes my program to break!

I see THIS QUESTION but how can I save each Id as string?

I'm using a global array to store the selected items' data so, anyway to save one attribute of a JSON in string?

I'm going to have (for example) 3 items and make another get request for each Id:

axios.get(`http://api.nemov.org/api/v1/Market/Symbol/${this.props.ID}`)

One of those Ids, is: 9481703061634967 but JS convert that to 9481703061634968 so the get request is broken!

Any solution?

MohamadKh75
  • 2,582
  • 5
  • 28
  • 54
  • `9481703061634967 > Number.MAX_SAFE_INTEGER` get request isn't broken - that number is too big - why have you mentioned JSON? the API returns XML not JSON – Bravo Oct 27 '18 at 11:15
  • 1
    `how can I save each Id as string` - when you parse the XML, don't change the Id from it's current type (a string, because everything in XML is a string) to integer - then your code won't break the data it gets from the API – Bravo Oct 27 '18 at 11:18
  • @Bravo - yeah, i mean that i can't make my other request which is dependent on the Id - how can i store that? consider that, that is not a single item, i have an array of objects which every one of them have Id with this long! – MohamadKh75 Oct 27 '18 at 11:18
  • 1
    you've shown ZERO code .. .so, I can't tell you which part of your code that parses the XML is breaking the ID - look for parseInt, parseFloat or even a unary + in your code – Bravo Oct 27 '18 at 11:19
  • i'm using `Axios` so i'm receiving JSON not XML – MohamadKh75 Oct 27 '18 at 11:19
  • you've shown XML, you haven't shown JSON - something must be converting the XML to JSON - it's not the server, it must be your code – Bravo Oct 27 '18 at 11:19
  • 1
    if you mean about the API link, if you use a browser, it will return XML but if you use `Axios` or anything like that, you will get JSON – MohamadKh75 Oct 27 '18 at 11:20
  • To summarize: The serverside can't be changed to return strings instead of integers, `JSON.parse()` cannot be customized to return ints instead of strings, so the only option I can see is a custom parser, which seems to be a bad idea. – Jonas Wilms Oct 27 '18 at 11:22
  • @JonasWilms - hmm... or maybe i can create a new array of Ids (Id is string) and copy every Id when receiving the response immediately - this will work? – MohamadKh75 Oct 27 '18 at 11:25
  • Have a look at https://github.com/datalanche/json-bignum/blob/master/README.md – Jonas Wilms Oct 27 '18 at 11:26
  • @JonasWilms - can i use that in React Native? – MohamadKh75 Oct 27 '18 at 11:27
  • 3
    You can tell Axios to fetch XML, not JSON (`responseType`)... – georg Oct 27 '18 at 11:28
  • just set `'Accept':'application/xml'` request header, and you'll get XML – Bravo Oct 27 '18 at 11:33

2 Answers2

0

See my solution on this question:

Transform the response to string, then apply a repalce with a regex to convert Id field to string type:

const axios = require("axios");
axios.get(url, {transformResponse: [data => data]}).then((response) => {
    let parsed = JSON.parse(response.data.replace(/"Id":(\d+),/g, '"Id":"$1",'))
    console.log(parsed)
});
-1

Use this:

let strId = this.props.ID.toString();
axios.get(`http://api.nemov.org/api/v1/Market/Symbol/${strId}`)
ficabj5
  • 111
  • 10
  • Not Working! - Consider that this is happening when i fetch data and save the response NOT when i pass the props! – MohamadKh75 Oct 27 '18 at 11:52