React,.js 错误,未定义为来自服务器的响应

React.js Error getting Undefined as response from Server

提问人:successAkin 提问时间:10/10/2023 最后编辑:Brian Tompsett - 汤莱恩successAkin 更新时间:10/10/2023 访问量:27

问:

我正在从node.js中的服务器获取一些数据到我的react.js数据正在从函数中成功获取,并且我在控制台中看到了它,但是当我想在我的组件中使用它时,我看到未定义。其他人获取我正在使用的函数可以正常工作; 只是那个特定的功能不起作用。 我的钩子.js文件中的取取函数:

export async function useFetchMyHomes(){
  const [myhomesData, setMyHomesData] = useState({ isLoadingMyHomesData: true,           myHomesApiData: null, myHomesStatus: null, myHomesServerError: null})

   useEffect(() => {
     const fetchMyHomesData = async () => {
         try {
             const {id} = await getUser();
            console.log('ID from myHouse', id)
             const { data, status } = await      axios.get(`/api/house/getMyhouse/${id}`)
            console.log('Saved House Data', data)
            console.log('status',status)
            if(status === 200){
                setMyHomesData({ isLoadingMyHomesData: false, myHomesApiData: data, myHomesStatus: status, myHomesServerError: null })
            }else{
                setMyHomesData({ isLoadingMyHomesData: false, myHomesApiData:   null, myHomesStatus: status, myHomesServerError: null })
            }
        } catch (error) {
            setMyHomesData({ isLoadingMyHomesData: false, myHomesApiData: null,      myHomesStatus: null, myHomesServerError: error })   
         }
     }
     fetchMyHomesData()
 }, [])

 return myhomesData
}

从上面的代码中,我很好地接收了来自后端的数据。

我要使用信息的组件:Myhomes

    function MyHomes({ toggle, isOpen }) {
     const [likedHouses, setLikedHouses] = useState([])
     const [currentPage, setCurrentPage] = useState(1);

    const { isLoading, apiData, serverError } = useFetch()

    const { isLoadingMyHomesData, myHomesApiData, myHomesStatus,    myHomesServerError} = useFetchMyHomes()
    const houseData = myHomesApiData?.data.saveHouseData

    console.log('MY HOUSE DTA', houseData)

这给了我未定义的输出。console.log('MY HOUSE DTA', houseData)

我已经正确检查了代码,但我仍然无法弄清楚问题所在。

reactjs react-hooks 组件 数据操作

评论


答: 暂无答案