Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

87
Vistas
JavaScript Accessing Data From Imported Script

So I'm trying to export/import script from model.js and I use this

import * as model from './model.js';

Here's the script of 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);
  }
};

This is the render part where I'm trying to access recipe part 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 trying to access recipe part here: const { recipe } = model.loadRecipe;

But I'm getting undefined. Please help me identify the problem, is it exporting, importing or I'm accessing data in the wrong way? Also, how should I push the recipe part to the state recipe?

Thank you very much.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

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);
    state.recipe = data.data.recipe;
    console.log(recipe);
  } catch (err) {
    console.error(err);
  }
};
await model.loadRecipe(id);
const { recipe } = model.state;

but why you dont want to return the data and save it to a variable instead? normally you will just return the recipe from the loadRecipe function.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can import the values from model.js separately.

import { state, loadRecipe } from './model';

Then, you can read the state value after running the loadRecipe() method.

// ...
await loadRecipe(id);
console.log(state.recipe);

But, I think you forget to set recipe value in loadRecipe() function in model.js too.

// get the recipe ...
// then ...
state.recipe = recipe;
about 4 years ago · Juan Pablo Isaza Denunciar

0

If you try it with vanilla JS:

// model.js
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 another file(call.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;

...in your html file...

<script type="text/javascript" src="model.js"></script> 
<script type="text/javascript" src="call.js"></script> 
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda