Struggling with exporting/importing script from model.js, I've used the following code:
import * as model from './model.js';
Below is the code snippet from model.js:
export const state = {
recipe: {},
};
console.log(state.recipe);
export const loadRecipe = async function (id) {
try {
const res = await fetch(
`https://forkify-api.herokuapp.com/api/v2/recipes/${id}`
);
const data = await res.json();
if (!res.ok) throw new Error(`${data.message} (${res.status})`);
console.log(data);
let { recipe } = data.data;
console.log(recipe);
} catch (err) {
console.error(err);
}
};
In the rendering part, I'm attempting to access the recipe section from model.js.
const showRecipe = async function () {
try {
const id = window.location.hash.slice(1);
if (!id) return;
renderSpinner(recipeContainer);
//1.Loading Recipe
await model.loadRecipe(id);
const { recipe } = model.loadRecipe.recipe;
I'm encountering an issue trying to access the recipe section here: const { recipe } = model.loadRecipe;
The value returned is undefined. Can you help me pinpoint the problem - whether it's related to importing, exporting, or accessing the data incorrectly? And how should I update the state.recipe with the recipe section?
Thank you for any assistance provided.