Error: 在 React Mongo DB 中无法读取属性 ‘map’ 的错误。

huangapple go评论82阅读模式
英文:

Error: Can not read property 'map' of undefined error In React Mongo DB

问题

After executing my React code, I got the following error message in the browser console: "TypeError: Cannot read property 'map' of undefined". here is my code:


const MyComponent = () => {
  const [data, setData] = useState([]);

  useEffect(() => {
    fetchData();
  }, []);

  const fetchData = async () => {
    const response = await fetch('https://api.example.com/data');
    const jsonData = await response.json();
    setData(jsonData);
  };

  return (
    <div>
      {data.map((item) => (
        <div key={item.id}>{item.name}</div>
      ))}
    </div>
  );
};

export default MyComponent;
英文:

After executing my React code, I gotthe following error message in the browser console: "TypeError: Cannot read property 'map' of undefined". here is my code


const MyComponent = () =&gt; {
  const [data, setData] = useState([]);

  useEffect(() =&gt; {
    fetchData();
  }, []);

  const fetchData = async () =&gt; {
    const response = await fetch(&#39;https://api.example.com/data&#39;);
    const jsonData = await response.json();
    setData(jsonData);
  };

  return (
    &lt;div&gt;
      {data.map((item) =&gt; (
        &lt;div key={item.id}&gt;{item.name}&lt;/div&gt;
      ))}
    &lt;/div&gt;
  );
};

export default MyComponent;

答案1

得分: 1

我建议您使用 try...catch 来处理可能出现的请求错误,以确保您的应用程序不会崩溃。示例代码如下:

const fetchData = async () => {
  try {
    const response = await fetch('https://api.example.com/data');
    const jsonData = await response.json();
    setData(jsonData);
  }
  catch (error) {
    // 如果请求返回错误...
    setData([]);
  }
}

如果错误仍然存在,可能是您的请求没有错误,但没有返回任何内容(undefined)。

英文:

I recommend that you use try...catch to handle possible errors in your request, so your app won't break. Like this:

const fetchData = async () =&gt; {
  try {
    const response = await fetch(&#39;https://api.example.com/data&#39;);
    const jsonData = await response.json();
    setData(jsonData);
  }
  catch (error) {
    // If the request returns an error...
    setData([]);
  }
}

If the error persists, probably your request has no error but is not returning anything (undefined).

答案2

得分: 1

You need to check first data should be defined when component renders.

const MyComponent = () => {
  const [data, setData] = useState([]);

  useEffect(() => {
    fetchData();
  }, []);

  const fetchData = async () => {
    const response = await fetch('https://api.example.com/data');
    const jsonData = await response.json();
    setData(jsonData);
  };

  return (
    <div>
      {data && data.length?.map((item) => (
        <div key={item.id}>{item.name}</div>
      ))}
    </div>
  );
};

export default MyComponent;
英文:
**You need to check first data should be defined when component renders **

 const MyComponent = () =&gt; {
    const [data, setData] = useState([]);

 useEffect(() =&gt; {
   fetchData();
 }, []);

 const fetchData = async () =&gt; {
  const response = await fetch(&#39;https://api.example.com/data&#39;);
  const jsonData = await response.json();
  setData(jsonData);
 };

 return (
    &lt;div&gt;
      {data &amp;&amp; data.length?.map((item) =&gt; (
        &lt;div key={item.id}&gt;{item.name}&lt;/div&gt;
      ))}
    &lt;/div&gt;
  );
};

export default MyComponent;

huangapple
  • 本文由 发表于 2023年5月17日 21:04:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272432.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定