Uncaught TypeError: Cannot read property 'length' of undefined jquery.datatables.js:1918

Uncaught TypeError: Cannot read property 'length' of undefined jquery.datatables.js:1918

strokesstrokes Posts: 8Questions: 0Answers: 0
edited September 2012 in DataTables 1.9
Hi guys.

My JSON Response is:
[code][{"id":"vammcxks","businessId":"fovkfslw","group":{"id":"Czrzep","name":"Willy","status":"OK","dateCreated":1306800420000},"name":"Jules","status":"OK","address":"89 S. Riverside Plaza", ...[/code]

Without root "aaData" which I guess is causing the problem because in jquery.datatables.js:1918 it's getting called 'aData.length';

In Chrome console: Uncaught TypeError: Cannot read property 'length' of undefined jquery.datatables.js:1918

How can I eliminate this problem? I cannot change much on server side, I'm using a framework there that returns the JSON response.

Thanks in advance.

Replies

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Since your response is just an array you need to use sAjaxDataProp to tell DataTables this fact - set it to an empty string (see the docs).

    Allan
  • strokesstrokes Posts: 8Questions: 0Answers: 0
    Thank you Allan.
    I have done that but still the exact same problem.
    Here's my table:
    [code]var oTable = $('#merchantsTable').dataTable({
    "bProcessing": true,
    "sAjaxDataProp":"",
    "bServerSide": true,
    "sAjaxSource": "${pageContext.request.contextPath}/"
    }); [/code]

    If I remove the "bServerSide", the request type becomes "text/html", which of course doesn't work.

    What else causes this problem?
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    If you are using server-side processing ( "bServerSide": true ) your server-side script needs to fully implement the protocol described here: http://datatables.net/usage/server-side

    Allan
  • strokesstrokes Posts: 8Questions: 0Answers: 0
    edited September 2012
    Thanks for the great help. Really appreciate it :)

    I'm close.

    I fixed server side and JSON now looks ok to me:
    (copied from Chrome)

    [code]aaData: [{id:vammcxks, businessId:fovkfslw,…}, {id:abbtorgp, businessId:qpehxtxt,…},…]
    iDisplayLength: 10
    iDisplayStart: 0
    sEcho: "10"
    [/code]

    But I have this message:

    "DataTables warning(table= id="merchantsTable"): Requested unknown parameter '0' from the data source for row 0"

    and the footer is

    "Showing 1 to NaN of NaN entries (filtered from NaN total entries)"
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    [quote]
    iDisplayLength: 10
    iDisplayStart: 0
    [/quote]

    The server doesn't send those parameters back, it sends iTotalDisplayRecords and iTotalRecords among others. Have a look at the page again :-)

    Also have you used mData to tell DataTables what property to read from the data source objects?

    Allan
  • strokesstrokes Posts: 8Questions: 0Answers: 0
    JSON response, fixed:

    [code]aaData: [{id:vammcxks, businessId:fovkfslw,…}, {id:abbtorgp, businessId:qpehxtxt,…},…]
    iTotalDisplayRecords: 10
    iTotalRecords: 500
    sEcho: "10"[/code]

    I tried mData, but still the same warning and the data misses.

    [code]var oTable = $('#merchantsTable').dataTable({
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "${pageContext.request.contextPath}/",
    "aoColumns" : [
    { "mData": "businessId" },
    { "mData": "name" },
    { "mData": "merchantGroup.2" },
    { "mData": "webAddress" },
    { "mData": "createdOn" },
    { "mData": "status" }
    ]
    }); [/code]

    merchantGroup - has nested values, i need the third one(name), hence { "mData": "merchantGroup.2" }.
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    > iTotalDisplayRecords: 10

    Is that correct? As the documentation says:

    > Total records, after filtering (i.e. the total number of records after filtering has been applied - not just the number of records being returned in this result set)


    > merchantGroup - has nested values, i need the third one(name), hence { "mData": "merchantGroup.2" }.

    As in it is an array? That should work. Can you please either give me a link or use the DataTables debugger so I can access the information needed to be able to answer the question :-)

    Allan
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Btw - do you rally need server-side processing? If you are using less than 50'000 rows it would be easier to use client-side processing...
  • strokesstrokes Posts: 8Questions: 0Answers: 0
    Allan, sorry for bothering but I guess others will learn too.

    iTotalDisplayRecords - is ok I guess because I'm returning the size of the list after getting from database.

    Here's the link of debugger http://debug.datatables.net/inibal

    Yes I need server-processing, I'm going to display very large lists.

    Thanks
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    iTotalDisplayRecords and iTotalRecords should only be different if there is a filter applied to the table. If there is no filter then they much be identical.

    However, about the united property, you've got:

    > { "mData": "merchantGroup.2" }.

    which would access array index 2, but your data source object for merchantGroup is an object, not an array:

    [code]
    "merchantGroup": {
    "id": "Czrzep",
    "name": "Willy",
    "status": "OK",
    "dateCreated": 1306800420000
    },
    [/code]

    So you need to use `merchantGroup.id` or something like that since `2` has no meaning for that object.

    Allan
  • strokesstrokes Posts: 8Questions: 0Answers: 0
    Ok. made these two last changes but unfortunately, it's the same message.

    "DataTables warning(table= id="merchantsTable"): Requested unknown parameter '0' from the data source for row 0"

    http://debug.datatables.net/anavad
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Ah - you are using DataTables 1.9.1. `mData` was called `mDataProp` back then. I'd suggest updating to 1.9.4.

    Allan
  • strokesstrokes Posts: 8Questions: 0Answers: 0
    That fixed it thanks for the great support :)

    I have another question but maybe I'll go with another thread.
This discussion has been closed.