4

I'm having an issue where my angular code is working in firefox but not in chrome.

The browser console prints this:

TypeError: undefined is not a function
    at setSelectedColor (http://localhost:3000/assets/products/controllers/mens_detail_controller.js?body=1:24:54)

This is my controller (notice the console.logs at where the failure is happening):

app.controller('MensDetailCtrl', ['$scope', '$resource', '$controller', 'Product', function($scope, $resource, $controller, Product) {
  $controller('ProductsDetailCtrl', {$scope: $scope});

  $scope.init = function(id, locale, selected_color_id)
  {
    $scope.product = Product.get({productId: id, locale: locale}, function(data) {
      var unsorted_products_colors = data.products_colors.filter(function(product_color) {
        return product_color.mens == true;
      });

      $scope.products_colors = unsorted_products_colors.sort(function(a, b) {
        return a.mens_sort_order > b.mens_sort_order;
      });

      setSelectedColor(selected_color_id);
    });

  }

  var setSelectedColor = function(selected_color_id) {
    console.log("ffff"); //prints
    if (selected_color_id) {
      console.log("eeeffff"); //prints
      // the error seems to happen at this next line, which is line 24:
      $scope.selected_color = $scope.products_colors.find(function(el) {
        return el.id == selected_color_id;
      });
    } else {
      console.log("hhffff");
      $scope.selected_color = $scope.products_colors[0];
    }
    console.log("ffffzzz"); // does NOT print

    $scope.selected_variant = $scope.selected_color.variants[0];
    $scope.sorted_product_images = $scope.selected_color.product_images.sort(function(a, b) {
      return a.sort_order > b.sort_order;
    });
    $scope.selected_image = $scope.sorted_product_images[0];
  }


}]);

Why does it work completely fine on Firefox but not on Chrome? How would I fix this?

== UPDATE == Even when I print out the value of $scope.products_colors right before the crash, it prints out an object in my browser console:

ffff
eeeffff 
[Object]0: Object$$hashKey: "004"color: Objectcolor_id: 32created_at: "2014-08-12T19:47:32.000Z"id: 91mens: truemens_sort_order: 0product_id: 15product_images: Array[2]updated_at: "2014-08-12T19:47:32.000Z"variants: Array[1]womens: truewomens_sort_order: 0__proto__: Objectlength: 1__proto__: Array[0]

== UPDATE 2 ==

Removing the .find(...) and replacing it with the array subscript fixes the error, but it's not fixing the bug because I need to grab elements with a certain attribute:

  var setSelectedColor = function(selected_color_id) {
    if (selected_color_id) {
      // $scope.selected_color = $scope.products_colors.find(function(el) {
      //         return el.id == selected_color_id;
      //       });
      $scope.selected_color = $scope.products_colors[0]
    } else {
      $scope.selected_color = $scope.products_colors[0];
    }

    $scope.selected_variant = $scope.selected_color.variants[0];
    $scope.sorted_product_images = $scope.selected_color.product_images.sort(function(a, b) {
      return a.sort_order > b.sort_order;
    });
    $scope.selected_image = $scope.sorted_product_images[0];
  }
9
  • Probably change var setSelectedColor = function(selected_color_id) { to function setSelectedColor (selected_color_id) { When are you calling init? But is this code shown is exactly how it is? Commented Sep 22, 2014 at 18:42
  • Nope no luck... Yeh this is my exact controller code (except for the comments) Commented Sep 22, 2014 at 18:44
  • What is the value of $scopes.products_colors right before the crash? The error points to column 54, where you're calling find, so that can help you narrow it down... does $scopes.products_colors have a find property in both firefox and chrome? Commented Sep 22, 2014 at 18:45
  • Even though i posted the comment before, the variable still should have got instantiated to the function reference as your controller is instantiated, s what i said is wrong. Did you clear you chrome cache and try once.? Commented Sep 22, 2014 at 18:45
  • I tried on two different computers and neither of them worked. @Blackhole it says Yes for Chrome, so I don't get what's going on Commented Sep 22, 2014 at 18:49

1 Answer 1

-2

I fixed it by replacing .find with .filter(...)[0]

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

1 Comment

Do not use filter, for finding just one item use array.some. filter will still iterate evenif it finds a match.

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.