0

I'm having a problem with "Cannot read property 'indexOf' of undefined". Below are the source codes. Appreciate your help.

$http.get("js/project/data.json").then(function(response) {
        //$scope.case = response.data.caseno;    
        $scope.case = response.data.caseno.map(function(elem) { 
            return elem.toLowerCase(); 
        }); 
    });

    console.log($scope.project.ProjectCaseNo.toLowerCase());

    if($scope.case.indexOf($scope.project.ProjectCaseNo.toLowerCase()) != -1 ){
        $scope.glyphicon = 'glyphicon-ok green';
        $scope.CaseMessage = 'Valid Case No.';
    }else{
        $scope.glyphicon = 'glyphicon-remove red';
        $scope.CaseMessage = 'Invalid Case No.';
    };
};
3

2 Answers 2

2

change it something like this:

    $scope.VerifyCaseNo = function(){
        $http.get("js/project/data.json").then(function(response) {
            //$scope.case = response.data.caseno;    
            $scope.case = response.data.caseno.map(function(elem) { 
                return elem.toLowerCase(); 
            }); 
            console.log($scope.project.ProjectCaseNo.toLowerCase());

            if($scope.case.indexOf($scope.project.ProjectCaseNo.toLowerCase()) != -1 ){
                $scope.glyphicon = 'glyphicon-ok green';
                $scope.CaseMessage = 'Valid Case No.';
            }else{
                $scope.glyphicon = 'glyphicon-remove red';
                $scope.CaseMessage = 'Invalid Case No.';
            };
        });
    };

Your $scope.case will be undefined before the $http.get() operation is complete. so you get the Cannot read property 'indexOf' of undefined error!

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

1 Comment

Thanks guys for the solution. It's working as expected. Need to understand more on the $http concept.
0

You can use String(yourValue).indexOf('')

2 Comments

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
@beryllium seems like an attempt to answer to me

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.