Express App + datatables, Cannot read property 'length' of undefined

Express App + datatables, Cannot read property 'length' of undefined

mazzofabmazzofab Posts: 1Questions: 0Answers: 0

hello to all and thanks for having me in the forum!

Trying to add rows dynamically from ajax/JSON but getting the above mentioned error.

My table is (PUG template) is defined on the page as:

table#tblcontacts.table.table-striped
    thead
        tr
            th
                | Contact Name                  
            th
                | Type
            th
                | Contact Info
            th
         tbody

In jQuery on document ready loading the table with:

          $("#tblcontacts").DataTable({
        ajax: '/contacts/id/' + $("#eid").val(),
        dataSrc: '',
        type: 'GET',
        columns: [
            { data: 'eccontactname' },          
            { data: 'ectname' },            
            { data: 'eccontact' },
            {
                sortable: false,
                "render": function ( data, type, full, meta ) {
                    return '<button class=\'btn btn-danger btn-delete form-control pull-right\' dbcolid=' + full.ecid + ' dbcolidval=' + full.ecid + ' dbtable=\'entity_contacts\'><b class=\'fa fa-trash-o\'></b></button>';
                }
            },  
        ]   
    })

The idea is to display data and have a button to delete for every row. Followed another post to try this.

The express route will return the following JSON:

[{"ecid":40,"eccontactname":"bob","eccontact":"bob@gg.co.tz","ectname":"EMAIL"},
{"ecid":41,"eccontactname":"John","eccontact":"0887655467","ectname":"MOBILE PHONE"}]

Seeking some help as I'm clueless!

Thanks

Replies

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    Your ajax is not correct

       ajax: {'/contacts/id/' + $("#eid").val(),
    
        dataSrc: '',
    
        type: 'GET'},
    
  • bindridbindrid Posts: 730Questions: 0Answers: 119

    ooops now my ajax is wrong

    ajax: {url: '/contacts/id/' + $("#eid").val(),
     dataSrc: '',
     type: 'GET'},
    
    
This discussion has been closed.