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

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

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:

  1. const MyComponent = () => {
  2. const [data, setData] = useState([]);
  3. useEffect(() => {
  4. fetchData();
  5. }, []);
  6. const fetchData = async () => {
  7. const response = await fetch('https://api.example.com/data');
  8. const jsonData = await response.json();
  9. setData(jsonData);
  10. };
  11. return (
  12. <div>
  13. {data.map((item) => (
  14. <div key={item.id}>{item.name}</div>
  15. ))}
  16. </div>
  17. );
  18. };
  19. 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

  1. const MyComponent = () =&gt; {
  2. const [data, setData] = useState([]);
  3. useEffect(() =&gt; {
  4. fetchData();
  5. }, []);
  6. const fetchData = async () =&gt; {
  7. const response = await fetch(&#39;https://api.example.com/data&#39;);
  8. const jsonData = await response.json();
  9. setData(jsonData);
  10. };
  11. return (
  12. &lt;div&gt;
  13. {data.map((item) =&gt; (
  14. &lt;div key={item.id}&gt;{item.name}&lt;/div&gt;
  15. ))}
  16. &lt;/div&gt;
  17. );
  18. };
  19. export default MyComponent;

答案1

得分: 1

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

  1. const fetchData = async () => {
  2. try {
  3. const response = await fetch('https://api.example.com/data');
  4. const jsonData = await response.json();
  5. setData(jsonData);
  6. }
  7. catch (error) {
  8. // 如果请求返回错误...
  9. setData([]);
  10. }
  11. }

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

英文:

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

  1. const fetchData = async () =&gt; {
  2. try {
  3. const response = await fetch(&#39;https://api.example.com/data&#39;);
  4. const jsonData = await response.json();
  5. setData(jsonData);
  6. }
  7. catch (error) {
  8. // If the request returns an error...
  9. setData([]);
  10. }
  11. }

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.

  1. const MyComponent = () => {
  2. const [data, setData] = useState([]);
  3. useEffect(() => {
  4. fetchData();
  5. }, []);
  6. const fetchData = async () => {
  7. const response = await fetch('https://api.example.com/data');
  8. const jsonData = await response.json();
  9. setData(jsonData);
  10. };
  11. return (
  12. <div>
  13. {data && data.length?.map((item) => (
  14. <div key={item.id}>{item.name}</div>
  15. ))}
  16. </div>
  17. );
  18. };
  19. export default MyComponent;
英文:
  1. **You need to check first data should be defined when component renders **
  2. const MyComponent = () =&gt; {
  3. const [data, setData] = useState([]);
  4. useEffect(() =&gt; {
  5. fetchData();
  6. }, []);
  7. const fetchData = async () =&gt; {
  8. const response = await fetch(&#39;https://api.example.com/data&#39;);
  9. const jsonData = await response.json();
  10. setData(jsonData);
  11. };
  12. return (
  13. &lt;div&gt;
  14. {data &amp;&amp; data.length?.map((item) =&gt; (
  15. &lt;div key={item.id}&gt;{item.name}&lt;/div&gt;
  16. ))}
  17. &lt;/div&gt;
  18. );
  19. };
  20. 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:

确定