My question may sound a bit unusual, but I am facing a rather strange problem.
I have been using javascript
's .map
and .reduce
functions to create custom search filters in my scope examples. Here is an example of the code I have been working on:
$http.get('config/get/getOrders.php', {cache: true}).then(function(response){
$scope.orders = response.data.orders.order;
$scope.orders.map( function addPlace(item) {
item.firstname = $scope.customers.reduce(function(a,customers){
return item.id_customer === customers.id ? customers.firstname : a;
}, '');
return item;
});
});
In the $http.get()
, I am fetching a JSON file containing order data. Each order has an id_customer
value, which corresponds to the customer who placed the order.
I wanted to enhance the search function by including the customer name
. For more information on this topic, you can refer to the question I posted yesterday.
Initially, it worked fine as the filter was able to search based on customer_firstname
. However, when I tried to apply a similar function in another controller with different scopes, I encountered a confusing error:
TypeError: Cannot read property 'reduce' of undefined
. Despite checking all the necessary data and configurations, such as $scope.products
, $scope.stock_availables
, and $scope.productCombinations
, I cannot seem to figure out why this issue is arising.
$http.get('config/get/getProducts.php', {cache: true}).then(function(response){
$scope.products = response.data.products.product;
$scope.products.map( function addPlace(item) {
item.eanCombination = $scope.productCombinations.reduce(function(a, productCombinations, stock_availables){
return item.id === stock_availables.id + stock_availables.id === stock_availables.id_product_attribute + stock_availables.id_product_attribute === productCombinations.id ? productCombinations.ean13: a;
}, '');
return item;
});
});
Summary: The function works in one controller but not in another.
If you have any inquiries, feel free to ask in the comments section.
Thank you in advance!