Angular Underscore Unable to read property 'find' of undefined

advertisements

I'm trying to return the object with a specific "ID" from the json service. I can see the objects in my console log but any attempt to iterate or use underscore _find gives me "undefined"

var metaresult = _.find($scope.results.results, function(rw){ return rw.id == $scope.answer1 });

gives me this error:

TypeError: Cannot read property 'find' of undefined
at k.$scope.metaResult (https://evening-taiga-2443.herokuapp.com/js/controllers/MainController.js:173:21)

Here is my factory:

 app.factory('results', ['$http', function($http) {
  return $http.get('js/services/results.json')
.success(function(data) {
  return data;
})
.error(function(err) {
  return err;
});

}]);

any my json structure:

{
"results": [
    {
        "id": "a"
    },
              .... and so on

EDIT: So the issue seems to be dependency. I tried using ng-underscore instead and I'm loading as follows:

<script src="js/ng-underscore.min.js"></script>

then the app.js

var app = angular.module("quizApp", ['720kb.socialshare', 'ngUnderscore']);

Then in my controller

app.controller('MainController', ['$scope', 'quiz', 'results', function($scope, quiz, results, underscore)

typing "underscore" into console gives me:

Uncaught ReferenceError: underscore is not defined

EDIT: Figured it out just need to use lodash


var metaresult = _.find( ...

TypeError: Cannot read property 'find' of undefined

This error means that javascript didn't know what is _. _ is undefined and when you do _.find then error is generated. It seems you're not loading libraries properly. Kindly make sure you have added the underscore js module. Once underscore is loaded, _ will reference to the js module and you will be able to use all the methods.