I am trying to get the data (id) from the main object inside an array that I GET from my Django-rest API.
I am going to use the id to pass it into a fetch url.
I have tried to use item.id but it doesn't get the id value.
This is the format of the array of the "task".
[
{
"title": "Task 4",
"description": "Task 4",
"video": "ad12312312",
"done": false,
"steps": [
{
"title": "Task 4 Task 1",
"description": "Task 4 Task 1",
"done": false,
"task_id": 4,
"id": 10
},
{
"title": "Task 4 Task 2",
"description": "Task 4 Task 2",
"done": false,
"task_id": 4,
"id": 11
},
{
"title": "Task 4 Task 3",
"description": "Task 4 Task 3",
"done": false,
"task_id": 4,
"id": 12
}
],
"id": 4
}
]
class Screen extends Component {
constructor() {
super();
this.state = {
task: [],
};
}
componentDidMount() {
AppState.addEventListener('change', this._handleAppStateChange);
fetch('https://url.com/daily-ecommerce/', {
method: 'GET',
headers: {
'Authorization': `Token xxxxx`
}
})
.then( res => res.json())
.then( jsonRes => this.setState({ task: jsonRes }) )
.catch( error => console.log(error))
.then(console.log(this.state.task.id))
fetch(`https://url.com/step/?task_id=${this.state.task.id}`, {
method: 'GET',
headers: {
'Authorization': `Token xxxxx`
}
})
I need to access the ID of the task. (in this case, the "id":4)
this.state.task[0].idshould workhttps://api.taskoftheday.com/step/?task_id=${this.state.task[0].id}, in some way?thenwiththis.state.task[0].id?