我试图通过我的reactapplication调用内部烧瓶API函数,
export default function ThermoBreastCancerIntroPage({ ...rest }) {
const [is_logged_in, set_is_logged_in] = useState('false');
React.useEffect(() => {
window.scrollTo(0, 0);
document.body.scrollTop = 0;
fetch('http://127.0.0.1:9000/api/is_logged_in/').then(results => results.json())
.then(data => {
const {name} = data.results[0];
set_is_logged_in(name.res);
console.log(is_logged_in);
});
}, []);
flask API返回如下响应:
return jsonify({'res':False})
但是我在浏览器中遇到以下错误:
Unhandled Rejection (TypeError): Cannot read property '0' of undefined
(anonymous function)
C:/ThermoAnalyser/material-kit-pro-react-v1.9.0/src/views/LandingPage/ThermoBreastCancerPage.js:37
34 | window.scrollTo(0, 0);
35 | document.body.scrollTop = 0;
36 | fetch('http://127.0.0.1:9000/api/is_logged_in/').then(results => results.json())
> 37 | .then(data => {
| ^ 38 | const {name} = data.results[0];
39 | set_is_logged_in(name.res);
40 | console.log(is_logged_in);
我不确定我在哪里做错了吗?
答案 0 :(得分:0)
您的诺言链中的某些情况是返回诺言拒绝或引发错误,应捕获并处理这两种情况。
fetch
ok
状态 results.json
您的处理逻辑
data.results
不是不是数组,或者是长度为0的数组,则尝试访问undefined属性也会引发错误catch
块代码
fetch('http://127.0.0.1:9000/api/is_logged_in/')
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
const {name} = data.results[0]; // <-- if this throws, is ok since now caught
set_is_logged_in(name.res);
})
.catch(error => {
console.error(error);
// any other error handling and accounting the app/component needs
});
set_is_logged_in(name.res);
console.log(is_logged_in);
排队的反应状态更新是异步的,并且在渲染周期之间进行处理,因此尝试在入队后立即记录“已更新”状态将仅记录来自当前的状态渲染周期,而不是您期望的 next 状态。当状态值更新时,您应该使用效果进行记录。
useEffect(() => {
console.log(is_logged_in);
}, [is_logged_in]);