koque
koque

Reputation: 2256

Angularsjs cannot find service function

My filterProducts function makes a call to findIntersection which is present but I get an error findIntersection is undefined.

angular.module('BrandService', [])
.service('BrandService', function ($filter, DataService) {
    var productDb;
    var products;

    return {
        filterProducts(brands, priceRange) {
            var filteredProducts = [];
            var brandProducts = [];
            var priceProducts = [];
            var productsArray = [];

            var brandChecked = false;
            var priceChecked = false;

            angular.forEach(brands, function (brand) {
                if (brand.checked) {
                    brandChecked = true;

                    angular.extend(brandProducts,
                        $filter('filter')(productDb, { 'brand': brand.name }));
                }

                if (brandChecked) {
                    productsArray.push(brandProducts);
                    console.log('brandProducts = ', brandProducts)
                }
            });

            angular.forEach(priceRange, function (price) {
                if (price.checked) {
                    priceChecked = true;

                    let filteredProductDb = productDb.filter((prod) => {
                        return (prod.price >= price.low && prod.price <= price.high);
                    });
                    angular.extend(priceProducts, filteredProductDb);    
                }
            });

            if (priceChecked) {
                productsArray.push(priceProducts);
                // console.log('priceProducts = ', priceProducts)
            }

            if (!brandChecked && !priceChecked) {
                filteredProducts = products;
            } else {
                if (productsArray.length > 1) {
                    filteredProducts = findIntersection(productsArray);
                } else {
                    filteredProducts = productsArray[0];
                }
            }

            return filteredProducts;
        },

        findIntersection(productsArray) {
            console.log('findIntersection called')
            var filteredProducts = [];
            var filteredSet = new Set();

            for(var i=0; i < productsArray.length - 1; i++) {
                var products1 = productsArray[i];
                var products2 = productsArray[i+1];

                angular.forEach(products1, function(product1) {
                    angular.forEach(products2, function(product2) {
                        if(product1._id == product2._id) {
                            filteredSet.add(product1);
                        }
                    });
                });
            }

            filteredProducts = Array.from(filteredSet);
            return filteredProducts;
        }            
    }        
})

My filterProducts function makes a call to findIntersection which is present but I get an error findIntersection is undefined.

My filterProducts function makes a call to findIntersection which is present but I get an error findIntersection is undefined.

Upvotes: 0

Views: 71

Answers (2)

korteee
korteee

Reputation: 2692

You have to make a reference to local object.

Simply change this: filteredProducts = findIntersection(productsArray);

to this: filteredProducts = this.findIntersection(productsArray);

Upvotes: 0

anotherdev
anotherdev

Reputation: 2567

You are returning a javascript object with properties. You are not defining global functions.

You need to store the service returned before :

var service = { findProducts: ... , findIntersection: ... };
return service; 

And instead of calling findIntersection, call service.findIntersection.

Upvotes: 1

Related Questions