how to solve "Uncaught TypeError: Cannot read property 'length' of undefined" this error.

how to solve "Uncaught TypeError: Cannot read property 'length' of undefined" this error.

maahi3391maahi3391 Posts: 1Questions: 1Answers: 0

var $table = $('#productListTable');
//execute the below code only where we have table
if($table.length){
console.log('Inside the table !');

    var jsonUrl = '';
    if (window.categoryId == ''){
        jsonUrl = window.contextRoot + '/json/data/all/products';
        console.log(jsonUrl);
    }
    else{
        jsonUrl = window.contextRoot + '/json/data/category/'+ window.categoryId +'/products';
        console.log(jsonUrl);
    }


   $table.DataTable({

       lengthMenu: [[3,5,10,-1], ['3 Records', '5 Records', '10 Records', 'All']],
       pageLength: 5,
      serverSide: true,
       ajax: {
           url: jsonUrl,
           datasrc: ''
       } ,

        columns: [
                 { data: 'name' },
                 { data: 'brand'},
                 { data: 'unitprice'},
                 { data: 'quantity'}
                 ] 

   });

Answers

This discussion has been closed.