Uncaught TypeError: Can not read property 'indexOf' of undefined

0

I have a somewhat strange situation, or I think I do not understand what is going on because it does not enter the if I am using the var this.searchParams.data where 3 situations happen

  • There's nothing
  • price_desc
  • appears
  • price_asc appears
  • Then I'm using the indexOf for the simple reason that we use ES5

    But it comes as an object

    {sortBy: "price_asc"}
    

    But no matter how hard you try that when it is not defined or does not exist it does not enter if to show the products ...

        this.searchParams = window.getSearch();
    
        this.searchSortBy = Object.values(this.searchParams.data)[0];
    
        console.log('-----------1-----------');
        console.log(typeof this.searchSortBy.indexOf('price') == null);
        console.log('-----------2-----------');
        console.log(this.searchSortBy.indexOf('price') === "undefined");
        console.log('-----------3-----------');
        console.log(typeof this.searchSortBy.indexOf('price') == null);
    
        if (!this.searchParams.data.price && typeof this.searchSortBy.indexOf('price') == null) {
          this.getProducts();
        }
    

    I would really appreciate the help.

        
    asked by Marco Montoya 05.09.2018 в 21:42
    source

    1 answer

    2

    I think you have a bad concept of how they work or how certain methods and functions are used.

    To start

      

    typeof: returns the type of the variable (operand) that you are   consulting

    some examples:

    typeof undefined //retorna 'undefined'
    typeof null //retorna 'object'
    
      

    indexOf: returns the index of the first occurrence it finds and if   can not find it returns -1

    now indexOf can be used in Strings and Arrays and if you use it in other types of variables (say numbers, booleans) it throws you the following error

      

    Uncaught TypeError: indexOf is not a function

    now if we go to the error that throws you:

      

    Uncaught TypeError: Can not read property 'indexOf' of undefined

    this basically means that somewhere in your code you did the following:

    undefined.indexOf(...)
    

    which means that most likely the following expression is giving you undefined

    this.searchSortBy // <--- esa variable no existe
    

    EDITING

    If we go to where the problem starts, it is in this line of code

    this.searchSortBy = Object.values(this.searchParams.data)[0];
    

    To make clear the use of Object.values you get a clear error if you do the following:

    // estas sentencias arrojan error "Cannot convert undefined or null to object"
    Object.values(null)
    Object.values(undefined)
    

    in view and considering that you do not get that error means the following

    this.searchParams.data // <- esta variable si existe
    // pero es probable que sea un objeto vacío, es decir
    this.searchParams.data = {};
    // lo que haría que
    Object.values(this.searchParams.data) 
    // lo anterior retornaría un arreglo vacío []
    // otra opción es que la propiedad data sea como lo siguiente
    this.searchParams.data = { a: undefined, b: 10};
    

    whatever your problem is the variable this.searchParams.data

        
    answered by 05.09.2018 в 22:52