如何修复我的代码中的fetch问题?我遇到了问题,无法继续完成这个网站。

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

How can i fix my fetch issue in my code im having issues and i cannot go on forward to complete this site

问题

在VS代码中,我遇到了以下错误信息:

> 获取失败 TypeError: 获取失败
>     在 getItems (http://localhost:3000/main.b060b3b85b623d5e4359.hot-update.js:47:25)
>     在 http://localhost:3000/main.b060b3b85b623d5e4359.hot-update.js:54:5
>     在 commitHookEffectListMount (http://localhost:3000/static/js/bundle.js:50177:30)
>     在 commitPassiveMountOnFiber (http://localhost:3000/static/js/bundle.js:51670:17)
>     在 commitPassiveMountEffects_complete (http://localhost:3000/static/js/bundle.js:51642:13)
>     在 commitPassiveMountEffects_begin (http://localhost:3000/static/js/bundle.js:51632:11)
>     在 commitPassiveMountEffects (http://localhost:3000/static/js/bundle.js:51622:7)
>     在 flushPassiveEffectsImpl (http://localhost:3000/static/js/bundle.js:53507:7)
>     在 flushPassiveEffects (http://localhost:3000/static/js/bundle.js:53459:18)
>     在 commitRootImpl (http://localhost:3000/static/js/bundle.js:53418:9)
英文:

So ill give you my error message:

> Failed to fetch TypeError: Failed to fetch
> at getItems (http://localhost:3000/main.b060b3b85b623d5e4359.hot-update.js:47:25)
> at http://localhost:3000/main.b060b3b85b623d5e4359.hot-update.js:54:5
> at commitHookEffectListMount (http://localhost:3000/static/js/bundle.js:50177:30)
> at commitPassiveMountOnFiber (http://localhost:3000/static/js/bundle.js:51670:17)
> at commitPassiveMountEffects_complete (http://localhost:3000/static/js/bundle.js:51642:13)
> at commitPassiveMountEffects_begin (http://localhost:3000/static/js/bundle.js:51632:11)
> at commitPassiveMountEffects (http://localhost:3000/static/js/bundle.js:51622:7)
> at flushPassiveEffectsImpl (http://localhost:3000/static/js/bundle.js:53507:7)
> at flushPassiveEffects (http://localhost:3000/static/js/bundle.js:53459:18)
> at commitRootImpl (http://localhost:3000/static/js/bundle.js:53418:9)

and heres my code on vs

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

import React, { useEffect, useState } from &quot;react&quot;;
import { useDispatch, useSelector } from &quot;react-redux&quot;;
import { Box, Typography, Tab, Tabs, useMediaQuery } from &quot;@mui/material&quot;;
import { setItems } from &quot;../../state&quot;;
import Item from &quot;../../components/item&quot;;

const ShoppingList = () =&gt; {
  const dispatch = useDispatch();
  const [value, setValue] = useState(&quot;all&quot;);
  const items = useSelector((state) =&gt; state.cart.items);
  const isNonMobile = useMediaQuery(&quot;(min-width:600px)&quot;);
  console.log(&quot;items&quot;, items);

  const handleChange = (event, newValue) =&gt; {
    setValue(newValue);
  };

  async function getItems() {
    const items = await fetch(
      &quot;https://localhost:1337/api/items?populate=image&quot;,
      { method: &quot;GET&quot; }
    );
    const itemsJson = await items.json();
    dispatch(setItems(itemsJson.data));
  }

  useEffect(() =&gt; {
    getItems();
  }, []); // eslint-disable-line react-hooks/exhaustive-deps

  const topRatedItems = items.filter(
    (item) =&gt; item.attributes.category === &quot;topRated&quot;
  );
  const newArrivalsItems = items.filter(
    (item) =&gt; item.attributes.category === &quot;newArrivals&quot;
  );
  const bestSellersItems = items.filter(
    (item) =&gt; item.attributes.category === &quot;bestSellers&quot;
  );

  return (
    &lt;Box width=&quot;80%&quot; margin=&quot;80px auto&quot;&gt;
      &lt;Typography variant=&quot;h3&quot; textAlign=&quot;center&quot;&gt;
        Our Featuted &lt;b&gt;Products&lt;/b&gt;
      &lt;/Typography&gt;
      &lt;Tabs
        textColor=&quot;primary&quot;
        indicatorColor=&quot;primary&quot;
        value={value}
        onChange={handleChange}
        centered
        TabIndicatorProps={{ sx: { display: isNonMobile ? &quot;block&quot; : &quot;none&quot; } }}
        sx={{
          m: &quot;25px&quot;,
          &quot;&amp; .MuiTabs-flexContainer&quot;: {
            flexWrap: &quot;wrap&quot;,
          },
        }}
      &gt;
        &lt;Tab label=&quot;ALL&quot; value=&quot;all&quot; /&gt;
        &lt;Tab label=&quot;NEW ARRIVALS&quot; value=&quot;newArrivals&quot; /&gt;
        &lt;Tab label=&quot;BEST SELLERS&quot; value=&quot;bestSellers&quot; /&gt;
        &lt;Tab label=&quot;TOP RATED&quot; value=&quot;topRated&quot; /&gt;
      &lt;/Tabs&gt;
      &lt;Box
        margin=&quot;0 auto&quot;
        display=&quot;grid&quot;
        gridTemplateColumns=&quot;repeat(auto-fill, 300px)&quot;
        justifyContent=&quot;space-around&quot;
        rowGap=&quot;20px&quot;
        columnGap=&quot;1.33%&quot;
      &gt;
        {value === &quot;all&quot; &amp;&amp;
          items.map((item) =&gt; (
            &lt;Item item={item} key={`${item.name}-${item.id}`} /&gt;
          ))}
        {value === &quot;newArrivals&quot; &amp;&amp;
          newArrivalsItems.map((item) =&gt; (
            &lt;Item item={item} key={`${item.name}-${item.id}`} /&gt;
          ))}
        {value === &quot;bestSellers&quot; &amp;&amp;
          bestSellersItems.map((item) =&gt; (
            &lt;Item item={item} key={`${item.name}-${item.id}`} /&gt;
          ))}
        {value === &quot;topRated&quot; &amp;&amp;
          topRatedItems.map((item) =&gt; (
            &lt;Item item={item} key={`${item.name}-${item.id}`} /&gt;
          ))}
      &lt;/Box&gt;
    &lt;/Box&gt;
  );

  return &lt;div&gt;shopping list&lt;/div&gt;;
};

export default ShoppingList;

<!-- end snippet -->

答案1

得分: 0

The error could be a network issue or a CORS (Cross-Origin Resource Sharing) issue.

If it's a CORS issue, you have to configure the server to allow requests from the domain and port that your React app is running.

英文:

The error could be a network issue or a CORS (Cross-Origin Resource Sharing) issue.

If its a CORS issue you have to configure the server to allow requests from the domain and port that your React app is running.

huangapple
  • 本文由 发表于 2023年5月10日 13:33:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76215186.html
匿名

发表评论

匿名网友

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

确定