英文:
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
问题
端点 "https://www.themealdb.com/api/json/v1/1/list.php?c=list"
返回以下 JSON 数据响应<sup>*</sup>:
{
"meals": [
{
"strCategory": "Beef"
},
{
"strCategory": "Breakfast"
},
{
"strCategory": "Chicken"
},
{
"strCategory": "Dessert"
},
{
"strCategory": "Goat"
},
{
"strCategory": "Lamb"
},
{
"strCategory": "Miscellaneous"
},
{
"strCategory": "Pasta"
},
{
"strCategory": "Pork"
},
{
"strCategory": "Seafood"
},
{
"strCategory": "Side"
},
{
"strCategory": "Starter"
},
{
"strCategory": "Vegan"
},
{
"strCategory": "Vegetarian"
}
]
}
<sup>*</sup>我不知道这些数据是否正确。我只能验证/确认它是否是所请求的数据。
我在你的 fetchRecipes
thunk 中看到的问题是它没有返回任何东西。
export const fetchRecipes = createAsyncThunk(
'recipes/fetchRecipes',
async () => {
// This fetch Promise isn't returned
fetch('https://www.themealdb.com/api/json/v1/1/list.php?c=list')
.then(response => response.json())
// console.log returns undefined
.then(data => console.log(data))
.catch(error => console.error(error));
}
);
建议的解决方案
- 从 Thunk 中返回一个值。
- 不要混合使用
async/await
和 Promise 链,选择一个使用即可。 - 不要忘记在
fetchRecipes.rejected
情况下返回任何抛出的(并捕获的)错误。请参阅 处理 Thunk 错误 以获取更多详细信息。
使用 Promise 链
export const fetchRecipes = createAsyncThunk(
'recipes/fetchRecipes',
(_, thunkAPI) => {
// 返回 Promise
return fetch('https://www.themealdb.com/api/json/v1/1/list.php?c=list')
.then(response => response.json())
.then(data => {
// 独立记录日志
console.log(data);
// 从 Promise 链中返回数据
return data;
})
.catch(error => {
console.error(error);
return thunkAPI.rejectWithValue(error);
);
}
);
使用 async/await
/try/catch
export const fetchRecipes = createAsyncThunk(
'recipes/fetchRecipes',
async (_, thunkAPI) => {
try {
const response = await fetch(
'https://www.themealdb.com/api/json/v1/1/list.php?c=list'
);
const data = await response.json();
console.log(data);
return data;
} catch(error) {
console.error(error);
return thunkAPI.rejectWithValue(error);
}
}
);
英文:
Issue
The endpoint "https://www.themealdb.com/api/json/v1/1/list.php?c=list"
returns the following JSON data response<sup>*</sup>:
{
"meals": [
{
"strCategory": "Beef"
},
{
"strCategory": "Breakfast"
},
{
"strCategory": "Chicken"
},
{
"strCategory": "Dessert"
},
{
"strCategory": "Goat"
},
{
"strCategory": "Lamb"
},
{
"strCategory": "Miscellaneous"
},
{
"strCategory": "Pasta"
},
{
"strCategory": "Pork"
},
{
"strCategory": "Seafood"
},
{
"strCategory": "Side"
},
{
"strCategory": "Starter"
},
{
"strCategory": "Vegan"
},
{
"strCategory": "Vegetarian"
}
]
}
<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(
'recipes/fetchRecipes',
async () => {
// This fetch Promise isn't returned
fetch('https://www.themealdb.com/api/json/v1/1/list.php?c=list')
.then(response => response.json())
// console.log returns undefined
.then(data => console.log(data))
.catch(error => 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(
'recipes/fetchRecipes',
(_, thunkAPI) => {
// Return Promise
return fetch('https://www.themealdb.com/api/json/v1/1/list.php?c=list')
.then(response => response.json())
.then(data => {
// log independently
console.log(data);
// return data from Promise chain
return data;
})
.catch(error => {
console.error(error);
return thunkAPI.rejectWithValue(error);
);
}
);
Using async/await
/try/catch
export const fetchRecipes = createAsyncThunk(
'recipes/fetchRecipes',
async (_, thunkAPI) => {
try {
const response = await fetch(
'https://www.themealdb.com/api/json/v1/1/list.php?c=list'
);
const data = await response.json();
console.log(data);
return data;
} catch(error) {
console.error(error);
return thunkAPI.rejectWithValue(error);
}
}
);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论