简体   繁体   English

我无法访问 JSON Object 值

[英]I can't access JSON Object values

Had these response below from server in console.在控制台中有以下来自服务器的响应。 However, i can't seem to access data[0].type or data[1].count.但是,我似乎无法访问 data[0].type 或 data[1].count。 It's giving me this error TypeError: Cannot read properties of undefined (reading 'type') .它给了我这个错误TypeError: Cannot read properties of undefined (reading 'type')

 (5)[{…}, {…}, {…}, {…}, {…}] 0: { type: 'hotel', count: 7 } 1: { type: 'apartments', count: 0 } 2: { type: 'resorts', count: 0 } 3: { type: 'villas', count: 0 } 4: { type: 'cabins', count: 0 } length: 5

Below is the function to get data from server using axios下面是 function 使用 axios 从服务器获取数据

 const useFetch = (url) => { const [data,setData] = useState([]); const [loading,setLoading] = useState(false); const [error,setError] = useState(false); useEffect(() => { const fetchData = async () => { setLoading(true); try{ await axios.get(url).then((res) => { setData(res.data); }) }catch(err){ setError(err); } setLoading(false); }; fetchData(); }, [url]); const reFetch = async() => { setLoading(true); try{ const res = await axios.get(url); setData(res.data); }catch(err){ setError(err); } setLoading(false); }; return {data,loading,error,reFetch}; }; export default useFetch;

if i try console.log(data[0]), it gives me the an object.如果我尝试 console.log(data[0]),它会给我一个 object。 But if i try console.log(data[0].type), it doesn't但是如果我尝试 console.log(data[0].type),它不会

 import "./propertyList.css"; import useFetch from '../../hooks/useFetch' import React from 'react'; const PropertyList = () => { const { data, error, loading } = useFetch("/hotels/countByType") const images = [ "https://cf.bstatic.com/xdata/images/xphoto/square300/57584488.webp?k=bf724e4e9b9b75480bbe7fc675460a089ba6414fe4693b83ea3fdd8e938832a6&o=", "https://cf.bstatic.com/static/img/theme-index/carousel_320x240/card-image-apartments_300/9f60235dc09a3ac3f0a93adbc901c61ecd1ce72e.jpg", "https://cf.bstatic.com/static/img/theme-index/carousel_320x240/bg_resorts/6f87c6143fbd51a0bb5d15ca3b9cf84211ab0884.jpg", "https://cf.bstatic.com/static/img/theme-index/carousel_320x240/card-image-villas_300/dd0d7f8202676306a661aa4f0cf1ffab31286211.jpg", "https://cf.bstatic.com/static/img/theme-index/carousel_320x240/card-image-chalet_300/8ee014fcc493cb3334e25893a1dee8c6d36ed0ba.jpg" ] console.log(data[0].type); return ( < div className = "pList" > { loading? ( "Loading" ): ( < > { data && images.map((img, i) => ( < div className = "pListItem" > < img src = { img } alt = "" className = "pListImg" / > < div className = "pListTitles" > { /* <h1>{data[i]?.type}</h1> <h2>{data[i]?.count} {data[i]?.type} </h2> */ } < /div> < /div> )) } < /> ) } < /div> ); }; export default PropertyList;

Possible two reasons for this are:可能的两个原因是:

  1. request end's up as error and "data" is undefined请求结束为错误并且“数据”未定义

  2. data is not an array (i would say that's it)数据不是数组(我会说就是这样)

    a) console.log(data), how does it look, is it an array? a) console.log(data),它看起来怎么样,是不是一个数组?

    b) it's probably not an array and there is something like a nested "data" array inside, so use that b)它可能不是一个数组,里面有一个嵌套的“数据”数组,所以使用它

Did you try converting it using JSON.parse() from string, maybe server is returning the response in string form.您是否尝试使用 JSON.parse() 从字符串转换它,也许服务器正在以字符串形式返回响应。 Also the condition you added data && images.map.... , here data will be true always还有你添加data && images.map....的条件,这里的data永远是真的

Can you show what you got after logging (data[0])?你能显示你在记录(数据[0])后得到了什么吗?

If you want to make sure your console.log statement doesn't have an error when accessing data[0].type then you can put the statement inside a useEffect like the following:如果您想确保您的console.log语句在访问data[0].type时没有错误,那么您可以将语句放在useEffect中,如下所示:

useEffect(() => {
  if (data) {
    console.log(data[0].type);
  }
}, [data]);

Hopefully this works for you.希望这对你有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM