Uncaught TypeError: Cannot read property 'length' of undefined while searching

Uncaught TypeError: Cannot read property 'length' of undefined while searching

inquisitive_sthainquisitive_stha Posts: 7Questions: 6Answers: 0

This working great and showing data as expected but showing error as soon as a key is pressed on search box. Any help would be appreciated.

Uncaught TypeError: Cannot read property 'length' of undefined
at ub (dataTables.min.js:108)
at dataTables.min.js:106
at i (dataTables.min.js:104)
at Object.success (dataTables.min.js:104)
at i (vendors.min.js:2)
at Object.fireWith [as resolveWith] (vendors.min.js:2)
at A (vendors.min.js:4)
at XMLHttpRequest.<anonymous> (vendors.min.js:4)

My code

    $( document ).ready(function() {
        $('#voterDataTable').DataTable( {
            "processing": true,
            "serverSide": true,

            "ajax": {
                "url":base_path +'/admin/van_voter_record/listVoter',
                "dataType":"json",
                "type":"POST",
                "data":{"_token":csrf_token}
            },
            "columns":[
            {"data":"name_voter"},
            {"data":"updated_at"},
            {"data":"created_at"},
            {"data":"action","searchable":false,"orderable":false}
            ]
        } );
    });

On controller

        $columns = array(
            0 => 'name_voter',
            1 => 'email_preferred',
            2 => 'created_at',
            3 => 'action'
        );

        $totalData = Van_voter_record::count();
        $limit = $request->input('length');
        $start = $request->input('start');
        $order = $columns[$request->input('order.0.column')];
        $dir = $request->input('order.0.dir');

        if(empty($request->input('search.value'))){
            $posts = Van_voter_record::offset($start)
            ->limit($limit)
            ->orderBy($order,$dir)
            ->get();
            $totalFiltered = Van_voter_record::count();
        }else{
            $search = $request->input('search.value');
            $posts = Van_voter_record::where('name_voter', 'like', "%{$search}%")
            ->orWhere('email_preferred','like',"%{$search}%")
            ->orWhere('created_at','like',"%{$search}%")
            ->offset($start)
            ->limit($limit)
            ->orderBy($order, $dir)
            ->get();
            $totalFiltered = Van_voter_record::where('name_voter', 'like', "%{$search}%")
            ->orWhere('email_preferred','like',"%{$search}%")
            ->count();
        }       


        $data = array();

        if($posts){

            foreach($posts as $r){

            $nestedData['name_voter'] = $r->name_voter;
            $nestedData['email_preferred'] = $r->email_preferred;
            $nestedData['created_at'] = date('d-m-Y H:i:s',strtotime($r->created_at));
            $nestedData['action'] = '
            <a href="'.url('/admin/van_voter_record/show').'/'.$r->id.'" class="btn btn-sm btn-secondary" title="View Details"><i class="fa fa-eye" aria-hidden="true"></i></a>
            &nbsp;&nbsp;
            <form method="POST" action="'.url('/admin/van_voter_record').'/'.$r->id.'" accept-charset="UTF-8" id="frm_'.$r->id.'" class="form form-delete-btn" title="Delete">
            <input name="_method" type="hidden" value="DELETE">
            <input name="_token" type="hidden" value="'.csrf_token().'">
            <button type="button" class="btn btn-sm btn-danger" data-toggle="modal" data-target="#confirm-delete'.$r->id.'"><i class="fa fa-trash" aria-hidden="true"></i></button>
            </form>

            <div class="modal fade confirm-delete" id="confirm-delete'.$r->id.'" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content">
            <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLongTitle"><strong>Confirm Delete</strong></h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
            </button>
            </div>
            <div class="modal-body">
            <p>You are about to delete one client <strong>"'.$r->name_voter.'"</strong>, this procedure is irreversible.</p>
            <p>Do you want to proceed?</p>
            <p class="debug-url"></p>
            </div>
            <div class="modal-footer">
            <button type="button" class="btn btn-warning" data-dismiss="modal">Cancel</button>
            <a onclick="submit_form(\'frm_'.$r->id.'\')" class="btn btn-primary btn-ok">Delete</a>
            </div>
            </div>
            </div>
            </div>
            ';
            $data[] = $nestedData;
        }
    }

    $json_data = array(
        "draw"          => intval($request->input('draw')),
        "recordsTotal"  => intval($totalData),
        "recordsFiltered" => intval($totalFiltered),
        "data"          => $data
    );

    echo json_encode($json_data);
    exit();

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Can you post the Ajax response too, please. At a quick look it appears that your returning email_preferred and not created_at

This discussion has been closed.