我有这个功能:
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))
}
在控制台中返回这个:
承诺 {[[PromiseStatus]]: “pending”, [[PromiseValue]]: undefined}
proto [[PromiseStatus]]:“已解决”
[[PromiseValue]] : Object
avatar_url : "https://avatars.githubusercontent.com/u/"
login : "hello world"
.
.
.
我需要访问对象中的名称/值对,但我无法访问它们。我假设在将响应转换为 json 但无法弄清楚之后我需要采取额外的步骤。如果有人可以提供帮助,将不胜感激!
原文由 Banner 发布,翻译遵循 CC BY-SA 4.0 许可协议
response.json()
返回一个承诺,所以你还需要适当地处理它,例如: