我认为这与在某处绑定this
和我试图遍历一个对象有关。在这段代码中,我可以看到响应是有效的,我只是在this.setState
行中得到了一个错误:_this2.setState is not a function.
componentDidMount() {
console.log('MatchesScreen: ', Object.keys(this.props))
Object.keys(this.props.profile.profile.matches).map(function(username, keyIndex) {
console.log('match', username)
url = 'https://xxxxx.execute-api.us-west-2.amazonaws.com/prod/profile?username=' + username
fetch(url, {
method: 'GET',
})
.then((response) => response.json())
.then((responseJson) => {
this.setState({isLoading: false})
this.props.updateMatches({matches: responseJson})
})
.catch((error) =>{
console.error(error);
})
})
}
通过比较,这段代码可以正常工作,我想是因为没有循环在进行吗?
componentDidMount() {
Auth.currentAuthenticatedUser({
bypassCache: false
}).then(user => {
username = 'username=' + user.username
this.setState({username: user.username})
url = 'https://xxxxxxx.execute-api.us-west-2.amazonaws.com/prod/profile?' + username
fetch(url, {
method: 'GET',
})
.then((response) => response.json())
.then((responseJson) => {
this.setState({
isLoading: false,
dataSource: responseJson,
age: responseJson.basic_info.age.toString(),
height: responseJson.basic_info.height,
promptQuestion: responseJson.prompt_question,
})
})
.catch((error) =>{
console.error(error);
});
})
.catch(err => console.log('EditProfileScreen: error getting user: ', err))
}