I have this function:
JavaScript
x
17
17
1
getUserData() {
2
fetch(this.props.apiUrl + this.state.username + '?client_Id=' + this.props.clientId + '&client_secret=' + this.props.clientSecret)
3
.then(function(response) {
4
var data = response.json();
5
this.setState({
6
userData: data
7
});
8
console.log(this.state.userData);
9
}.bind(this))
10
.catch(function(error) {
11
this.setState({
12
username: null
13
});
14
console.log(error)
15
}.bind(this))
16
}
17
Which returns this in the console:
Promise {[[PromiseStatus]]: “pending”, [[PromiseValue]]: undefined}
proto [[PromiseStatus]] : “resolved”
JavaScript
1
7
1
[[PromiseValue]] : Object
2
avatar_url : "https://avatars.githubusercontent.com/u/"
3
login : "hello world"
4
.
5
.
6
.
7
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!
Advertisement
Answer
response.json()
returns a promise, so you also need to handle it appropriately, eg:
JavaScript
1
7
1
.then(function(response) {
2
return response.json();
3
})
4
.then(function(parsedData) {
5
// data here
6
})
7