2

Im trying to search if a value exits in an array. Im using ui-select to capture the values, and i create function to verify if the value exists and all works great but the console show me many times Cannot read property 'indexOf' of undefined.

here the select codes used to capture the data

  <ui-select multiple ng-model="entrevistainicialModel.personalidad"
                           ng-disabled="false"
                           search-enabled="true"
                           append-to-body="true"
                           class="form-control ">
                    <ui-select-match placeholder="Comportamientos">
                        {{$item.comportamiento}}
                    </ui-select-match>
  <ui-select-choices repeat="multipleItem.idcomportamiento as multipleItem in datosJson[0].comportamientos | filter: $select.search">
                        {{multipleItem.comportamiento}}
                    </ui-select-choices>
                </ui-select>

here is the checkbox code with the function

<td class="text-center"><input type="checkbox" ng-checked="check(entrevistainicialModel.personalidad, 1)" value="1"></td>

and here it's the function

        $scope.check = function (data, n) {
        if (data.indexOf(n) !== -1) {
            return true;
        } else {
            return false;
        }
    }

all works great! only the console send me many times the error of indexOf

1
  • 1
    it's inicialized as array Commented Aug 17, 2017 at 19:08

2 Answers 2

1

If you can not read indexOf of data it is because data is undefined.

I wanted to say that data have no value in it so it can not have methods like indexOf. You can check like this:

console.log (typeof data);

Here, you try to read the type of value in data. If the result is undefined it's mean that data is empty.

So, you have to check if data is not empty (undefined). You can check it like that:

$scope.check = function (data, n) {
    if (!data) {return;}      // If data is undefined return.
    if (data.indexOf(n) !== -1) {
        return true;
    } else {
        return false;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

It's because data contains undefined, its because of what you are passing is undefined or from somewhere else you are passing nothing to this method. As per your code, I can say entrevistainicialModel.personalidad should be initialized with an array or string or something like tha, which have the indexOf method, then you will not get this error Cannot read property 'indexOf' of undefined

Otherwise you should handle the scenario like (data || []).indexOf(...)

4 Comments

I´m initializing entrevistainicialModel as array. Then i use the variable to put json data. And entrevistainicialModel.personalidad it´s another array into entrevistainicialModel
from where did you initializing? can you add a fiddle or plunker?
i just had to put this on the function if (!data) {return;} thanks for the help!
Yeah, !data means its undefined or null ir false equvalent, that meant its not initialized properly (what my guess was). (data || []).indexOf will also handle your failure, however it dosen't matter how you are handling that scenario. Better to not allowing that to happen.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.