【发布时间】:2019-10-31 12:41:04
【问题描述】:
我认为这与在某处绑定 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))
}
【问题讨论】:
标签: javascript function react-native scope this