简体   繁体   中英

TypeError: Cannot read property 'reduce' of undefined in angularjs

So my question may come up as a bit weird but the problem i'm having is actually very strange aswell.

So for creating custom search filters i'm using javascript 's function of .map and .reduce in this case i'm using these in my scope examples. Take a look at the following code:

$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;
        });
    });

So in the $http.get() i'm requesting a JSON file with data. This data consists of some orders with information. All these orders have a id_customer value. This value is connects to the customer information whom placed the order.

So i wanted to widen my search function with that customer name For more information about how and why take a look at this question i've asked yesterday.

And it worked. The filter could also search the customer_firstname . But then i wanted to use the same type of function in another controller. The function goal stays the same. Connecting data from multiple $scope types. But weird as it is the return is TypeError: Cannot read property 'reduce' of undefined . Yes i've placed this in the same app.js, but just in another controller. i've checked if all the data of $scope.products and $scope.stock_availables and $scope.productCombiantions exists and it does, i've checked the providers and etc and all suits fine to me so i have no clue why this is happening. The function in this case is:

$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;
    });
});

Short summary: A function does work in one controller but the same function doesn't work in another controller.

If you have any questions please ask them in the comments.

As always, Thanks in advance!

So i made a mistake in the sequence in wich all data is loaded. Because of the fact that the $scope.productCombinations was loaded after the products sequence. So tip for everyone. Always check the sequence in wich your code is written.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM