Answer the question
In order to leave comments, you need to log in
React/Redux on page throws undefined added array to object?
Hello, I have the following code
export function fetchCategories() {
return async dispatch => {
dispatch(fetchCategoriesStart())
try {
const response = await axios.get('/api/category/categories')
const categories = []
categories.push(response.data.data)
categories[0].forEach((category) => {
axios.get('/api/product/findById/'+ category._id)
.then(response => category.allProducts = response.data.products)
})
console.log(categories)
dispatch(fetchCategoriesSuccess(categories))
} catch (e) {
dispatch(fetchCategoriesError(e))
}
}
}
Answer the question
In order to leave comments, you need to log in
This line is synchronous
const response = await axios.get('/api/category/categories')
axios.get('/api/product/findById/'+ category._id)
.then(response => category.allProducts = response.data.products)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question