gpt4 book ai didi

javascript - react promise - TypeError : Cannot read property 'then' of undefined

转载 作者:行者123 更新时间:2023-11-30 09:32:17 24 4
gpt4 key购买 nike

我正在从以下调用中获取数据,该调用返回一个对象:

function getPersonList() {
const api = 'myapistring';
axios.get(api).then(res => {
console.log(res);
}).catch(err => console.log(err));
}

1 - 但是当我点击我的 componentDidMount 时。 promise 违背了,我不知道为什么。

2- 此外,由于响应是一个对象,我是否通过将初始状态设置为空 [ ] 做错了什么?-我不确定将其设置为对象的语法是什么。

const App = React.createClass({
getInitialState() {
return {
personList: [],
visiblePersonList: []
};
},
componentDidMount() {
console.log(getPersonList(response));
getPersonList().then((data) =>
this.setState({
data,
visiblePersonList: data
}));
//return getPersonList;
},
.....

谢谢大家!

最佳答案

你没有从 getPersonList 中返回任何东西

function getPersonList() {
const api = 'myapistring';
return axios.get(api).then(res => { // FIX THIS LINE WITH return
console.log(res);
}).catch(err => console.log(err));
}

关于javascript - react promise - TypeError : Cannot read property 'then' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45554553/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com