C
C
constant1012020-01-29 17:55:19
React
constant101, 2020-01-29 17:55:19

ReactJS: How to assign the result of fetch(list of objects) to variables?

js newbie: You need to use the storeProducts(object array) and detailProduct(specific object) variables later in the program, but they are not visible outside of getDetails. How to solve this dilemma? I tried to assign without an asynchronous function and await, but in this case, instead of an array of objects, Promise {} is returned.

let tempProducts = [];


async function getDetails(products, product){
  const storeProducts = await fetch('https:/products/').then(res => res.json());
  const detailProduct = await fetch('https:/products/:id').then(res => res.json());
}

getDetails();

Object.keys(storeProducts/*undefined*/).forEach(item => tempProducts.push({ ...item })
  );

const initialState = {
  products: tempProducts,
  productDetails: { ...detailProduct/*undefined*/ }
};

Thanks for the answer, sorry for the banality of the question

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
abberati, 2020-01-29
@constant101

Everything is in the dock

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question