I am trying to fetch data from an api which should return a json object with an array of recipes. I am using React hooks on the front end and Axios to perform the request. The hook runs but I get an error while trying to access the json array from the response.
Here's the sample api response:
{
count: 30,
recipes: [objects, ... 29]
}
and my code to get the recipe response:
const fetchRecipes = async () => {
try {
const recipeData = await Axios.get(api);
console.log(recipeData);
const recipes = await recipeData.json(); // error occurs here
setRecipes(recipes);
setLoading(false);
} catch (err) {
console.log(`Error occurred fetching recipes ${err}`)
}
};
useEffect(() => {
fetchRecipes();
}, []);
I have removed the hooks declarations and api url is https://api.myjson.com/bins/t7szj
. The problem lies in getting the array from the response. Is there something wrong with my api call?
This is the error message:
Error occurred fetching recipes TypeError: recipeData.json is not a function