Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

85
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!