Odd Length Error

Odd Length Error

inktinkt Posts: 18Questions: 3Answers: 0

I am getting an error about length, I tried google searching it, but I couldn't figure it out.

Error:
Uncaught TypeError: Cannot read property 'length' of undefined

Code:

$objDatatable = $(".bulkSMSTable").DataTable({
            ajax: {
                url: "/manager/bulk_sms/bulk_sms.php?jsFunction=populateTable",
                type: "POST",
                contentType: 'application/json',
                dataSrc: function (ajaxOutput) {
                    console.log(ajaxOutput);
                },
                error: function (jqXHR, exception, error) {
                    console.log('Error status:', jqXHR);
                    console.log('Exception: ' + exception);
                    console.log('Error message: ' + error);
                }
            },
            "columns": [
                {
                    "data": "date",
                    "title": "Date",
                    "orderable": true,
                    "width": "100px"
                },
                {
                    "data": "from",
                    "title": "From",
                    "orderable": true,
                    "width": "100px"
                },
                {
                    "data": "name",
                    "title": "Name",
                    "orderable": true
                },
                {
                    "data": "number",
                    "title": "Number",
                    "orderable": true,
                    "width": "100px"
                },
                {
                    "data": "message",
                    "title": "Message",
                    "orderable": false
                },
                {
                    "data": "status",
                    "title": "Status",
                    "orderable": true,
                    "width": "100px"
                }
            ],
            "dom": "<'row'<'col-sm-12 col-md-6'l><'col-sm-12 col-md-6'f>><'row'<'col-sm-12'tr>><'row'<'col-sm-12 col-md-5'i><'col-sm-12 col-md-7'p>>",
            "length": 50,
            "order": [[0, 'desc']]
        });

What am I doing wrong?

Answers

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736

    Guessing the problem is this:

                    dataSrc: function (ajaxOutput) {
                        console.log(ajaxOutput);
                    },
    
    

    When using ajax.dataSrc as a function you need to return something like the examples show. The error is likely caused by not returning anything.

    Kevin

  • inktinkt Posts: 18Questions: 3Answers: 0

    it isreturning data. ajaxOutput contains a JSON object. I used dataSrc to se what the ajax call was returning, is that not it's purpose? If not, how do I see the output?

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736

    You can use ajax.dataSrc as a function to see the return JSON. The purpose of ajax.dataSrc is to provide Datatables to the JSON data when its not at the expected data location. You also need to return the row data location as shown in the examples. If you don't return the data then the function will return undefined - the function is a standard Javascript function.

    Kevin

  • inktinkt Posts: 18Questions: 3Answers: 0

    How do I see the returned data, and specify "columns"?

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736

    Without seeing your data its hard to say exactly but assuming its in the expected data property you would do this:

    dataSrc: function (ajaxOutput) {
        console.log(ajaxOutput);
        return ajaxOutput.data;
    },
    

    Your columns will be defined based on your data structure. We can provide a more specific answer if you post an example of your data.

    Kevin

Sign In or Register to comment.