Answer the question
In order to leave comments, you need to log in
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*/ }
};
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question