网络请求失败错误React Native,使用Flask Python作为后端

问题描述 投票:0回答:1

使用邮递员,后端似乎正在获取正确的数据,但是当我将其放入提取中时,组件不会重新渲染状态,并且还给我一个黄色错误,网络请求失败。 后端由 Flask Python 组成,前端使用带有 android 模拟器的 Expo CLI,我查阅了以前的问题,发现一些说模拟器和机器未连接,我通过执行远程/本地 ip ex 修复了该问题:192.168.1....但它仍然返回网络请求失败。

import React, { useEffect, useState } from "react";
import { ScrollView, View, TouchableOpacity, Text } from "react-native";
import { connect } from "react-redux";
import * as actions from "../src/actions";
import EarningsBar from "./EarningsBar";

const DayCalendar = (props) => {
  const [isLoaded, setisLoaded] = useState(false);
  useEffect(() => {
    props.loadStock("daily");
    if (Object.keys(props.dailyStocks).length > 0) {
      setisLoaded(true);
      console.log(props.dailyStocks);
    }
  }, []);
  return (
    <ScrollView nestedScrollEnabled={true}>
      {isLoaded === true &&
        props.dailyStocks.time.map(
          ({ companyshortname, epsactual, epsestimate, ticker }, index) => {
            return (
              <EarningsBar
                key={index}
                companyName={companyshortname}
                companyAbbrev={ticker}
                companyEPS={epsestimate}
                companyRev={"$3.28"}
                companyActualEPS={epsactual}
                companyActualRES={"$5.66"}
                companyGrowthEPS={"103.3%"}
                companyGrowthRev={"83.8%"}
                arrow={"good"}
              />
            );
          }
        )}
    </ScrollView>
  );
};

const mapStateToProps = (state) => {
  return {
    dailyStocks: state.calendar.daily,
  };
};

export default connect(mapStateToProps, actions)(DayCalendar);

这是日历组件 这是动作函数

export const loadStock = (stock) => {
  return (dispatch) => {
    try {
      fetch(`http://192.168.1.13:3000/${stock}stock`)
        .then((response) => {
          return response.json();
        })
        .then((data) => {
          dispatch({ type: "LOAD_STOCKS", payload: { stock, data } });
        });
    } catch (error) {
      console.log(error);
    }
  };
};

我的减速器状态/和开关情况

export const initialLoginState = {
  users: [],
  searchedStocks: [],
  calendar: {
    daily: [],
    weekly: [],
    monthly: [],
  },
  stocks: [],
  searchTerm: null,
  isLoading: true,
  user_id: null,
  username: null,
  password: null,
  confirmPassword: null,
  email: null,
  birthdate: null,
  question: null,
  answer: null,
  userToken: null,
  isValidUser: true,
  isValidPassword: true,
};

export default (prevState = initialLoginState, action) => {
  switch (action.type) {
  case "LOAD_STOCKS":
      return {
        ...prevState,
        calendar: {
          [action.payload.prop]: action.payload.value,
        },
      };
    default:
      return {
        ...prevState,
      };
   }
};

如果您想查看完整的存储库,我已将其链接到下面。我已经尝试解决这个问题很长一段时间了,我只需要一个新的视角来看待它,非常感谢您的宝贵时间。 https://github.com/JayPankajPatel/EarningsWhisper

react-native react-redux fetch-api
1个回答
-1
投票

打开CMD并运行ipconfig,然后复制你的IP地址

在你的 Flask 代码中,假设 app.py

将 app.run() 更改为

app.run(主机=你的IP地址)

© www.soinside.com 2019 - 2024. All rights reserved.