I’m not getting the Json data - React Native

Asked

Viewed 26 times

1

I am unable to extract the data from the api.

Here I get the data:

  fetch(config.baseURL + '/calendario/situacao_horarios_mes/TESTE/2020/12')
     .then(res => res.json())
     .then(resJson => {            
         this.setState({ datas: resJson || []});
     }).catch(e => console.log(e));

     console.log(this.state.datas);

Result returned in console.log();

Array [
  Object {
    "dia": "2020-12-01",
  },
  Object {
    "dia": "2020-12-02",
  },
  Object {
    "dia": "2020-12-03",
  },
  Object {
    "dia": "2020-12-04",
  },
  Object {
    "dia": "2020-12-05",
  },
  Object {
    "dia": "2020-12-06",
  },
  Object {
    "dia": "2020-12-07",
  },
  Object {
    "dia": "2020-12-08",
  },
  Object {
    "dia": "2020-12-09",
  },
  Object {
    "dia": "2020-12-10",
  },
  Object {
    "dia": "2020-12-11",
  },
  Object {
    "dia": "2020-12-12",
  },
  Object {
    "dia": "2020-12-13",
  },
  Object {
    "dia": "2020-12-14",
  },
  Object {
    "dia": "2020-12-15",
  },
  Object {
    "dia": "2020-12-16",
  },
  Object {
    "dia": "2020-12-17",
  },
  Object {
    "dia": "2020-12-18",
  },
  Object {
    "dia": "2020-12-19",
  },
  Object {
    "dia": "2020-12-20",
  },
  Object {
    "dia": "2020-12-21",
  },
  Object {
    "dia": "2020-12-22",
  },
  Object {
    "dia": "2020-12-23",
  },
  Object {
    "dia": "2020-12-24",
  },
  Object {
    "dia": "2020-12-25",
  },
  Object {
    "dia": "2020-12-26",
  },
  Object {
    "dia": "2020-12-27",
  },
  Object {
    "dia": "2020-12-28",
  },
  Object {
    "dia": "2020-12-29",
  },
  Object {
    "dia": "2020-12-30",
  },
  Object {
    "dia": "2020-12-31",
  },
]

I wanted in the this.state.datas return only the date. Example: 2020-12-01 and so on.

In the alert I can only [object][object] or undefined.

  • Do you want an array only with the dates? Why don’t you make one .map just returning the dates?

  • I can use the . map outside the render?

  • Try something like let apenasDatas = resJson.map(el => el.dia); and then this.setState({ datas: apenasDatas || []});

  • Obtive esse resultado 
 2020-12-01,2020-12-02,2020-12-03,2020-12-04,2020-12-05,2020-12-06,2020-12-07,2020-12-08,2020-12-09,2020-12-10,2020-12-11,2020-12-12,2020-12-13,2020-12-14,2020-12-15,2020-12-16,2020-12-17,2020-12-18,2020-12-19,2020-12-20,2020-12-21,2020-12-22,2020-12-23,2020-12-24,2020-12-25,2020-12-26,2020-12-27,2020-12-28,2020-12-29,2020-12-30,2020-12-31 2020-12-03 ...

  • 1

    It worked, I was with a conversion to string without realizing it. Thank you very much for the help.

1 answer

0

Resolved with the help of the Cmte Cardinal

Final Code

fetch(config.baseURL + '/calendario/situacao_horarios_mes/TESTE/2020/12')
     .then(res => res.json())
     .then(resJson => {            
     let apenasDatas = resJson.map(el => el.dia); 
     this.setState({ datas: apenasDatas || []}); 
     }).catch(e => console.log(e));

     console.log(this.state.datas);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.