0

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

You have to make a reference to local object.

Simply change this: filteredProducts = findIntersection(productsArray);

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.