简体   繁体   English

未捕获的类型错误:无法读取未定义(读取“过滤器”)JavaScript 的属性

[英]Uncaught TypeError: Cannot read properties of undefined (reading 'filter') JavaScript

I have a problem concern filter in JavaScript can't working.我有一个问题,担心 JavaScript 中的过滤器无法工作。 I'm not sure may because API using long time for response我不确定可能是因为 API 使用很长时间进行响应

My code :我的代码:

const getDataAll  = () =>{
    const [machines, setMachines] = useState([])
    const getMc = async () =>{
       try {
         const resMc = await axios.get("My API")
         setMachines(resMc.data)
       } catch (err) {
         console.error(err.message)
       }

    useEffect(()=>{
       getMc()
    },[])
    
    const sumData = () =>{
       const filterName = machines.data.filter((el) => el.name == "v10turbo")
    }
}

Filter have show this alert in some time.过滤器已在一段时间内显示此警报。

在此处输入图像描述

And i see time show in browser for API response around 8-9 second follow image below我在浏览器中看到 API 响应的时间显示在 8-9 秒左右,如下图所示

在此处输入图像描述

I'm not sure if I got it right.我不确定我是否做对了。 if right please help me with to solve this problem如果正确,请帮我解决这个问题

The code itself is correct.代码本身是正确的。 But when your component renders, there is no data yet, useeffect works after rerender.但是当你的组件渲染时,还没有数据,useeffect在重新渲染后起作用。 You need a loading state:你需要一个加载状态:

 const getDataAll = () =>{ const [machines, setMachines] = useState([]); const [loading, setLoading] = useState(true); const getMc = async () =>{ try { const resMc = await axios.get("My API") setMachines(resMc.data) setLoading(false); } catch (err) { console.error(err.message) } finally { setLoading(false); } } useEffect(()=>{ getMc() },[]) const sumData = () =>{ const filterName = machines.data.filter((el) => item.name == "v10turbo") } } if(loading) { return<p>Loading...</p> }

This should fix it.这应该解决它。

I not sure, but this can be helpful我不确定,但这可能会有所帮助

const sumData = useCallback(() =>{
   const filterName = machines.data?.filter((el) => item.name == "v10turbo")
}, [machines]);

暂无
暂无

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

相关问题 未捕获的类型错误:无法读取 null 的属性(正在读取“切片”)------ 未捕获的类型错误:无法读取未定义的属性(正在读取“过滤器”) - Uncaught TypeError: Cannot read properties of null (reading 'slice') ------ Uncaught TypeError: Cannot read properties of undefined (reading 'filter') JavaScript(React)- 未捕获的类型错误:无法读取未定义的属性(读取“样式”) - JavaScript(React)- Uncaught TypeError: Cannot read properties of undefined (reading 'style') JavaScript - 未捕获(承诺)TypeError:无法读取未定义的属性(读取“状态”) - JavaScript - Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'status') 未捕获的类型错误:无法读取未定义的属性(读取“8”) - Uncaught TypeError: Cannot read properties of undefined (reading '8') 未捕获的类型错误:无法读取未定义的属性(读取“0”) - Uncaught TypeError: Cannot read properties of undefined (reading '0') 未捕获的类型错误:无法读取未定义的属性(读取“0”) - Uncaught TypeError: Cannot read properties of undefined (reading '0') 未捕获的类型错误:无法读取未定义的属性(读取“”) - Uncaught TypeError: Cannot read properties of undefined (reading '') 未捕获的类型错误:无法读取未定义的属性(读取“过滤器”)。 我找不到错误在哪里 - Uncaught TypeError: Cannot read properties of undefined (reading 'filter'). I cannot find the where is error Uncaught TypeError TypeError:无法读取未定义的属性(读取“路径”) - Uncaught TypeError TypeError: Cannot read properties of undefined (reading 'path') 未捕获的类型错误:无法读取未定义的属性(读取“目标”)和(读取“值”) - Uncaught TypeError: Cannot read properties of undefined (reading 'target') & (reading 'value')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM