beginerdeveloper beginerdeveloper - 2 years ago 1184
Javascript Question

Using datatable with node.js

I am trying to use jquery Datatable with Node.js.
Here is my Code HTML

<button id="btnSearch" type="submit" class="btn btn-responsive"><i class="icon-search"></i>&nbsp;Search</button>

<div class="box-body table-responsive no-padding">
<div id="tableContainer">
<table class="table table-hover" id="dataTables1">
<th class="text-left">base</th>
<th class="text-left">base1</th>
<div class="text-center">
<img id="loading-image" src="../dist/img/loading_spinner.gif" style="display: none;" />

Here is script code

<script src="//"></script>
<script type="text/javascript">
$("#btnSearch").click(function () {

function RefreshTable(tableId) {

table = $(tableId).dataTable();
var table2 = $(tableId).dataTable({
"bServerSide": true,
"bProcessing": true,
"responsive": true,
"bAutoWidth": false,
oLanguage: {
sProcessing: "<img src='../dist/img/loading_spinner.gif'/>"
"aLengthMenu": [10, 30, 50, 100],
"pageLength": 30,
"sAjaxSource": '',
"fnServerData": function (sSource, aoData, fnCallback) {
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": aoData,
"success": fnCallback
"sDom": "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'dataTables_wrapper'ip>>",
"sPaginationType": "full_numbers",//"full_numbers",
"aoColumns": [
{ "sName": "base", "bSortable": false },//0
{ "sName": "base1", "bSortable": false }

And here is my code server side node js

var async = require('async'),
QueryBuilder = require('datatable');

var tableDefinition = {
sTableName: 'Orgs'

var queryBuilder = new QueryBuilder(tableDefinition);

// requestQuery is normally provided by the DataTables AJAX call
var requestQuery = {
iDisplayStart: 0,
iDisplayLength: 5

// Build an object of SQL statements
var queries = queryBuilder.buildQuery(requestQuery);

exports.test = function(req, res){

userExport.getUserEdit(req.query.schemename, function(rows){

When i clicked in the search button then in the server side i recieved schemename is 'abc' and got the user but i can't response the json to table. In the tab console of browser i got a error :
jquery.dataTables.js:4108 Uncaught TypeError: Cannot read property 'length' of undefined, anyone help me out of this error or suggest me a solution to fix this problem thank for adventure

Answer Source

Add sAjaxDataProp: 'data' to your initialization params :

var table2 = $(tableId).dataTable({
  sAjaxDataProp: 'data',
  sAjaxSource: '',

Reason: When using sAjaxSource, dataTables 1.9.x expect data to be in the format { "aaData" : [...] }, thats why you get the Uncaught TypeError: Cannot read property 'length' of undefined. However, node-datatable exports the rows as { "data" : [...] }.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download