Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
81
rated 0 times [  82] [ 1]  / answers: 1 / hits: 29241  / 8 Years ago, wed, august 3, 2016, 12:00:00

I have this function:



  getUserData() {
fetch(this.props.apiUrl + this.state.username + '?client_Id=' + this.props.clientId + '&client_secret=' + this.props.clientSecret)
.then(function(response) {
var data = response.json();
this.setState({
userData: data
});
console.log(this.state.userData);
}.bind(this))
.catch(function(error) {
this.setState({
username: null
});
console.log(error)
}.bind(this))
}


Which returns this in the console:



Promise {[[PromiseStatus]]: pending, [[PromiseValue]]: undefined}



proto
[[PromiseStatus]] : resolved



[[PromiseValue]] : Object
avatar_url : https://avatars.githubusercontent.com/u/
login : hello world
.
.
.


I need to access the name/value pairs in the object but I can't get to them. I'm assuming I need to take one extra step after I convert the response to json but can't figure it out. If anyone could help it would be greatly appreciated!


More From » reactjs

 Answers
57

response.json() returns a promise, so you also need to handle it appropriately, eg:



.then(function(response) {
return response.json();
})
.then(function(parsedData) {
// data here
})

[#61168] Monday, August 1, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
iyannae

Total Points: 147
Total Questions: 88
Total Answers: 120

Location: Japan
Member since Sat, Jun 6, 2020
4 Years ago
;