Error: DataTables cannot read the property 'length' of an undefined value during an ajax request

I am encountering an issue while using DataTables with an AJAX endpoint. When I initialize the datatable on my site, I consistently receive an error message stating "Cannot read property 'length' of undefined". I have attempted to troubleshoot this code without success

Here is the JSON result from the request

{
    "data": [
        {
            "id": "28",
            "id_transaction": "xxxxxxxxxxxxxxxxx",
            "code": "xxxxxxxxxxxxxxxxxxxxx",
            "code_id": "xxx",
            "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0f777777777777777777774f603d217f63">[email protected]</a>",
            "status": "In Progress",
            "xxyy": "10",
            "xxyya": "xxxxxxx",
            "xxaa": "xxxxxxx",
            "xxx": "xxxxxxxxx",
            "created_at": "2019-05-01 20:51:31"
        },
        {
            ...

Here is the configuration of the datatable

var mytable = jQuery('#Transactions').dataTable( {
                ajax: {
                    url: '<?php echo admin_url( 'admin-ajax.php' ) ?>',
                    type: 'POST',
                    data: {action:'get_transactions'},
                    dataSrc: ''
                }
                columns: [
                    {"data": "id"},
                    {"data": "id_transaction"},
                    {"data": "code"},
                    {"data": "code_id"},
                    {"data": "email"},
                    {"data": "status"},
                    {"data": "xxyy"},
                    {"data": "xxyya"},
                    {"data": "xxaa"},
                    {"data": "xxx"},
                    {"data": "created_at"}
                ]
            } );

Here is the HTML for the table

<table id="Transactions" class="display" style="width:100%">  
    <thead>
      <th>ID</th>
      <th>ID Transaction</th>
      <th>Code</th>
      <th>Code ID</th>
      <th>Email</th>
      <th>status</th>
      <th>xxyy</th>
      <th>xxyya</th>
      <th>xxaa</th>
      <th>xxx</th>
      <th>created_at</th>

    </thead>
    <tbody>

  </tbody>
  <tfoot>
      <th>ID</th>
      <th>ID Transaction</th>
      <th>Code</th>
      <th>Code ID</th>
      <th>Email</th>
      <th>status</th>
      <th>xxyy</th>
      <th>xxyya</th>
      <th>xxaa</th>
      <th>xxx</th>
      <th>created_at</th>

   </tfoot>
</table>

Can anyone provide insights into why this error is occurring?

Thank you

Wiktor

Answer №1

make sure to include the "pageLength": 10, parameter when using datatables()

var mytable = jQuery('#Transactions').dataTable( {
               "pageLength": 10, //10 just an example
                ajax: {
                    url: '<?php echo admin_url( 'admin-ajax.php' ) ?>',
                    type: 'POST',
                    data: {action:'get_transactions'},
                    dataSrc: ''
                }
                columns: [
                    {"data": "id"},
                    {"data": "id_transakcji"},
                    {"data": "kod"},
                    {"data": "id_kodu"},
                    {"data": "email"},
                    {"data": "status"},
                    {"data": "xxyy"},
                    {"data": "xxyya"},
                    {"data": "xxaa"},
                    {"data": "xxx"},
                    {"data": "data_utworzenia"}
                ]
} );

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Setting up a function in React to utilize an image stored in state

I have a function in my react component for image classification that retrieves the image from the img tag using document.getElementById: const img = document.getElementById('animal_image');. The image uploaded via the file input updates the sta ...

What is the best way to close all modal dialogs in AngularJS?

Back in the day, I utilized the following link for the old version of angular bootstrap: https://angular-ui.github.io/bootstrap/#/modal var myApp = angular.module('app', []).run(function($rootScope, $modalStack) { $modalStack. dismissAll( ...

Establishing a Geolocation Object in HTML5

My HTML5 app relies on Geolocation. In cases where the user denies permission for geolocation, I want to pass a default position object (latitude:0, longitude:0) to ensure the app can still function. I understand how to detect permission denial, but I&apo ...

Exploring the art of div transitions and animations using React and Tailwind CSS

I successfully implemented the sidebar to appear upon clicking the hamburger icon with a transition. However, I am now facing the challenge of triggering the transition when the close button is clicked instead. I have attempted using conditional operations ...

The attempt to test the AngularJS application using the Jasmine plugin was unsuccessful

I'm currently in the process of learning how to write test cases for my angularJS application. As a beginner, I'm searching for some sample examples of working demos. I came across an example online that uses the Jasmine plugin to test an angular ...

Guide on creating a nested commenting platform with express.js

My goal is to set up a system for displaying multi-level comments, where a comment can have replies, which in turn can also have replies, and so on. Currently, I am using SQLite for this purpose. Below is the approach I have taken so far: Model const Comme ...

Guidance sought on utilizing a callback to retrieve data from mysql using node.js and ejs

I am trying to retrieve the value of foo from the query provided below: exports.get = function(id, cb) { sql = 'SELECT `sidebar`, `test` FROM users WHERE `id` = "' + id + '"'; con.query(sql, function(err, foo) { if (err) ...

How can I create a fading trail effect in Three.js that diminishes over time?

I am interested in achieving a similar effect to the example I found on this website: However, my goal is to have the old trail gradually fade into the background over time instead of cluttering the screen with persistent marks. I discovered that by usin ...

how to choose the :after pseudo-element with jQuery

Below are the codes I have tried. When this popup appears, I want to be able to close the entire popbox using the close button. CSS code .bigdiv{ display:none; background-color:#efefef; box-shadow: 10px 10px 10px 100000px rgba(0, 0, 0, 0.4); ...

What are the risks of using react + bootstrap without importing bootstrap.js?

When bootstrap library is used along with React, potential unpredictable behavior and difficult bugs may arise due to DOM modifications. Is it feasible to use bootstrap without including bootstrap.js? What are the drawbacks? While I am aware of the ' ...

The selection elements fail to reset correctly

I'm currently working on an Angular 4 application where I have a form that includes a select element inside a box. <div class="form-group"> <label for="designation">Designation</label> <select [class.red-borde ...

Resolving the bothersome complications of self-looping steps in jQuery animate delay

My timeline definition includes selectors and a sequence of delays and animations to apply to an object. I have also provided the option to loop through the steps for a specific object. Here is the function that I use to queue the animations: function an ...

Method in Vue.js is returning an `{isTrusted: true}` instead of the expected object

I am having an issue with my Vue.js component code. When I try to access the data outside of the 'createNewTask' function, it renders correctly as expected. However, when I attempt to log the data inside the function, it only returns {isTrusted: ...

Prevent User Access from Profile

I am interested in setting up a feature to block users. I already have the button ready. <a type="button" value="1" name="block" Cursor="pointer" href="blockuser.php?uid='. $data['id'].'">Block</a> My goal is to implement ...

While trying to install express using Node.js, an error (error:0906D06C :PEM routines : PEM_read_bio npm) occurred

I recently installed node.js on my computer and npm came along with it. However, when I try to execute "npm install express" in my Windows command prompt, I encounter the following error message. Can anyone guide me on how to resolve this issue? C:\U ...

AngularJS does not clear the array after a POST request is made

ISSUE Greetings! I am encountering an odd behavior with my backend. When I submit data, everything works flawlessly. However, if I press ENTER and submit an empty field, it reposts the previous value. Initially, I cannot submit an empty field, but after e ...

What is the best way to alternate between two CSS stylesheets when using jQuery-UI?

Just getting started with jQuery and javascript and decided to test out 2 different jQuery-ui stylesheets in a simple .html file. The goal is to have the example dialog open with the "flick" stylesheet and the datepicker within the dialog open with the "u ...

Altering the JavaScript variable by selecting an option from a dropdown list

After downloading a choropleth map from leafletjs.com, I encountered an issue with multiple JS files labeled by year (e.g. us-states2012.js, us-states2013.js). The challenge now is to implement a drop down menu in such a way that selecting a specific year ...

Displaying content conditionally in Vue JS upon the initial page load

I am currently working on a Vue component that is supposed to render the first time a page is opened. However, I am facing some confusion regarding the logic behind this. My approach involves using localStorage to check for an item called 'wasVisited& ...

Content is missing from the response of the AngularJS $http request

Attempting to retrieve content from a website using Angular's $http service. The request seems to be successful as console.log(response.data) displays most of the content but is missing some parts. Check out my code below: $http = angular.element(doc ...