Currently, I am facing an issue while attempting to execute an HTTP request and then store the output into my array.
This is what I have so far:
Ctrl
for (var i=0; i < numOfProduct.length; i++) {
productFactory.makeRequest(numOfProduct[i].id)
.then(function(data) {
console.log(i)
// This always displays 10, which corresponds to numOfProduct.length
try {
numOfProduct[i].push({detail: data});
} catch (error) {
// I am encountering a 'Cannot read property 'push' of undefined' error
}
})
$scope.numOfProduct = numOfProduct;
}
productFactory
service.makeRequest = function(id) {
return $http.get('/api/product/get' + id);
}
return service;
In essence, my objective is to include the result of an HTTP request as an object within each element of numOfProduct. However, due to some issues with my current HTTP request code, I am unable to achieve this. Any suggestions or assistance would be greatly appreciated. Thank you!