Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update model.js(adding if statement in createRecipeObject) #418

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions 18-forkify/final/src/js/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,27 @@ const createRecipeObject = function (data) {
};
};

// Here I made some changes for the createRecipeObjec and add simple if statement which can be really helpful for the catching error if something didnt download in a proper way. See code below, please
const createRecipeObject = function (data) {
console.log(data);
if (!data || !data.data || !data.data.recipe) {
console.error('Invalid data structure: ', data);
return;
}
const {recipe} = data.data;
return {
id: recipe.id,
title: recipe.title,
publisher: recipe.publisher,
sourceUrl: recipe.source_url,
image: recipe.image_url,
servings: recipe.servings,
cookingTime: recipe.cooking_time,
ingredients: recipe.ingredients,
...(recipe.key && {key: recipe.key})
};
};

export const loadRecipe = async function (id) {
try {
const data = await AJAX(`${API_URL}${id}?key=${KEY}`);
Expand Down