I am trying to fetch api from meal db website, it says the error failed to fetch API in the console

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

I am trying to fetch api from meal db website, it says the error failed to fetch API in the console

问题

Sure, here's the translated part of your message:

"请查找我的 recipeslice.js 代码。我正在使用 react-redux 技术制作食谱网站。我在从网站 https://www.themealdb.com/api.php 获取 API 数据方面遇到问题。

请帮助我从 https://www.themealdb.com/api.php 获取 API 数据,因为我正在使用 react-redux 技术制作食谱网站。

我尝试在 recipeslice.js 文件中添加代码,但控制台中的错误是获取数据失败。

我尝试在 recipeslice.js 文件中添加代码,但控制台中的错误是获取数据失败,还有更多错误,如:bundle.js:327 GET https://www.themealdb.com/api/json/v1/1/list.php?c=list net::ERR_INSUFFICIENT_RESOURCES。"

英文:

Please find my recipeslice.js code. I am making recipe website using react-redux technology. I am facing issue in fetching api from the website of "https://www.themealdb.com/api.php"

import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
import axios from 'axios';

export const fetchRecipes = createAsyncThunk(
  'recipes/fetchRecipes',
  async () => {
    fetch('https://www.themealdb.com/api/json/v1/1/list.php?c=list')
      .then(response => response.json())
      .then(data => console.log(data))
      .catch(error => console.error(error));
  }
);

const recipesSlice = createSlice({
  name: 'recipes',
  initialState: {
    recipes: [],
    status: 'idle',
    error: null,
  },
  reducers: {},
  extraReducers: (builder) => {
    builder
      .addCase(fetchRecipes.pending, (state) => {
        state.status = 'loading';
      })
      .addCase(fetchRecipes.fulfilled, (state, action) => {
        state.status = 'succeeded';
        state.recipes = action.payload;
      })
      .addCase(fetchRecipes.rejected, (state, action) => {
        state.status = 'failed';
        state.error = action.error.message;
      });
  },
});

export default recipesSlice.reducer;

Please help me to fetch api data from "https://www.themealdb.com/api.php" as I am making recipe website in react-redux technology.

I tried to add code in recipeslice.js file but the error in console is failed to fetch data.

I tried to add code in recipeslice.js file but the error in console is failed to fetch data and some more errors are this: bundle.js:327 GET https://www.themealdb.com/api/json/v1/1/list.php?c=list net::ERR_INSUFFICIENT_RESOURCES.

答案1

得分: 1

问题

端点 &quot;https://www.themealdb.com/api/json/v1/1/list.php?c=list&quot; 返回以下 JSON 数据响应<sup>*</sup>:

{
  &quot;meals&quot;: [
    {
      &quot;strCategory&quot;: &quot;Beef&quot;
    },
    {
      &quot;strCategory&quot;: &quot;Breakfast&quot;
    },
    {
      &quot;strCategory&quot;: &quot;Chicken&quot;
    },
    {
      &quot;strCategory&quot;: &quot;Dessert&quot;
    },
    {
      &quot;strCategory&quot;: &quot;Goat&quot;
    },
    {
      &quot;strCategory&quot;: &quot;Lamb&quot;
    },
    {
      &quot;strCategory&quot;: &quot;Miscellaneous&quot;
    },
    {
      &quot;strCategory&quot;: &quot;Pasta&quot;
    },
    {
      &quot;strCategory&quot;: &quot;Pork&quot;
    },
    {
      &quot;strCategory&quot;: &quot;Seafood&quot;
    },
    {
      &quot;strCategory&quot;: &quot;Side&quot;
    },
    {
      &quot;strCategory&quot;: &quot;Starter&quot;
    },
    {
      &quot;strCategory&quot;: &quot;Vegan&quot;
    },
    {
      &quot;strCategory&quot;: &quot;Vegetarian&quot;
    }
  ]
}

<sup>*</sup>我不知道这些数据是否正确。我只能验证/确认它是否是所请求的数据。

我在你的 fetchRecipes thunk 中看到的问题是它没有返回任何东西。

export const fetchRecipes = createAsyncThunk(
&#39;recipes/fetchRecipes&#39;,
async () =&gt; {
// This fetch Promise isn&#39;t returned
fetch(&#39;https://www.themealdb.com/api/json/v1/1/list.php?c=list&#39;)
.then(response =&gt; response.json())
// console.log returns undefined
.then(data =&gt; console.log(data))
.catch(error =&gt; console.error(error));
}
);

建议的解决方案

  • 从 Thunk 中返回一个值。
  • 不要混合使用 async/await 和 Promise 链,选择一个使用即可。
  • 不要忘记在 fetchRecipes.rejected 情况下返回任何抛出的(并捕获的)错误。请参阅 处理 Thunk 错误 以获取更多详细信息。

使用 Promise 链

export const fetchRecipes = createAsyncThunk(
&#39;recipes/fetchRecipes&#39;,
(_, thunkAPI) =&gt; {
// 返回 Promise
return fetch(&#39;https://www.themealdb.com/api/json/v1/1/list.php?c=list&#39;)
.then(response =&gt; response.json())
.then(data =&gt; {
// 独立记录日志
console.log(data);
// 从 Promise 链中返回数据
return data;
})
.catch(error =&gt; {
console.error(error);
return thunkAPI.rejectWithValue(error);
);
}
);

使用 async/await/try/catch

export const fetchRecipes = createAsyncThunk(
&#39;recipes/fetchRecipes&#39;,
async (_, thunkAPI) =&gt; {
try {
const response = await fetch(
&#39;https://www.themealdb.com/api/json/v1/1/list.php?c=list&#39;
);
const data = await response.json();
console.log(data);
return data;
} catch(error) {
console.error(error);
return thunkAPI.rejectWithValue(error);
}
}
);
英文:

Issue

The endpoint &quot;https://www.themealdb.com/api/json/v1/1/list.php?c=list&quot; returns the following JSON data response<sup>*</sup>:

{
  &quot;meals&quot;: [
    {
      &quot;strCategory&quot;: &quot;Beef&quot;
    },
    {
      &quot;strCategory&quot;: &quot;Breakfast&quot;
    },
    {
      &quot;strCategory&quot;: &quot;Chicken&quot;
    },
    {
      &quot;strCategory&quot;: &quot;Dessert&quot;
    },
    {
      &quot;strCategory&quot;: &quot;Goat&quot;
    },
    {
      &quot;strCategory&quot;: &quot;Lamb&quot;
    },
    {
      &quot;strCategory&quot;: &quot;Miscellaneous&quot;
    },
    {
      &quot;strCategory&quot;: &quot;Pasta&quot;
    },
    {
      &quot;strCategory&quot;: &quot;Pork&quot;
    },
    {
      &quot;strCategory&quot;: &quot;Seafood&quot;
    },
    {
      &quot;strCategory&quot;: &quot;Side&quot;
    },
    {
      &quot;strCategory&quot;: &quot;Starter&quot;
    },
    {
      &quot;strCategory&quot;: &quot;Vegan&quot;
    },
    {
      &quot;strCategory&quot;: &quot;Vegetarian&quot;
    }
  ]
}

<sup>*</sup>I have no idea if this data is correct. I can only verify/validate it is the data that was requested.

The issue I see with your fetchRecipes thunk is that it doesn't return anything.

export const fetchRecipes = createAsyncThunk(
&#39;recipes/fetchRecipes&#39;,
async () =&gt; {
// This fetch Promise isn&#39;t returned
fetch(&#39;https://www.themealdb.com/api/json/v1/1/list.php?c=list&#39;)
.then(response =&gt; response.json())
// console.log returns undefined
.then(data =&gt; console.log(data))
.catch(error =&gt; console.error(error));
}
);

Suggested Solution

  • Return a value from the Thunk.
  • Don't mix async/await with Promise chains, use one or the other.
  • Don't forget to also return any thrown (and caught) errors for the fetchRecipes.rejected case. See Handling Thunk Errors for more details.

Using Promise chain

export const fetchRecipes = createAsyncThunk(
&#39;recipes/fetchRecipes&#39;,
(_, thunkAPI) =&gt; {
// Return Promise
return fetch(&#39;https://www.themealdb.com/api/json/v1/1/list.php?c=list&#39;)
.then(response =&gt; response.json())
.then(data =&gt; {
// log independently
console.log(data);
// return data from Promise chain
return data;
})
.catch(error =&gt; {
console.error(error);
return thunkAPI.rejectWithValue(error);
);
}
);

Using async/await/try/catch

export const fetchRecipes = createAsyncThunk(
&#39;recipes/fetchRecipes&#39;,
async (_, thunkAPI) =&gt; {
try {
const response = await fetch(
&#39;https://www.themealdb.com/api/json/v1/1/list.php?c=list&#39;
);
const data = await response.json();
console.log(data);
return data;
} catch(error) {
console.error(error);
return thunkAPI.rejectWithValue(error);
}
}
);

huangapple
  • 本文由 发表于 2023年5月25日 20:02:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332054.html
匿名

发表评论

匿名网友

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

确定