data.items is undefined; can't access its "length" property!

data.items is undefined; can't access its "length" property!

dos4everdos4ever Posts: 4Questions: 3Answers: 0

Hello, can anyone to tell me where is my erorr in this cod please.

function loadTable() {

$.ajax({
    url: "/rest/Inventory/List",
    method: "GET",
  dataType: "json",
  success: function(data, items) {

     var dataSet = [];

      for(var i = 0; i < data.items.length; i++) {
            dataSet.push([
                data.items[i].id,
                data.items[i].device,
                data.items[i].type.name,
                data.items[i].additionalInfo,
                data.items[i].serialNumber,
                data.items[i].person.name,
                data.items[i].location.name
            ]);
        }

        $('#data-table').DataTable( {
            data: dataSet,
            columns: [
                { title: "Id" },
                { title: "Device" },
                { title: "Type" },
                { title: "Additional Info" },
                { title: "Serial Number" },
                { title: "Person" },
                { title: "Location" }
            ]
        } );

  }
});

}

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 20,308Questions: 26Answers: 4,769
    Answer ✓

    I would guess that the data parameter passed to your success function doesn't have an items object resulting in data.items being undefined. Without seeing whats in data its hard to say. You can use console.log(data); in the success function to find out.

    Instead of processing the data object (assuming that it is an array of objects) maybe you can use this example to just use the data as you receive it:
    https://datatables.net/examples/ajax/deep.html

    Kevin

  • colincolin Posts: 15,144Questions: 1Answers: 2,586

    From the error, I'm guessing the data object doesn't contain another object called items...

  • dos4everdos4ever Posts: 4Questions: 3Answers: 0

    thank you for your answer, i got the error, it was in the column type and person and location cuz in the "MYSQL" data base i have for this three tables id and name, and it was in my Datatables some rows on the type was empty, i got the right code, is her.

    function loadTable() {

    $.ajax({
        url: "/rest/Inventory/List",
        method: "GET",
      dataType: "json",
      success: function(r) {
         var data = r.data 
         var dataSet = [];
          //console.log(data.data);
          for(var i = 0; i < data.length; i++) {
    
                if (data[i].type == undefined)
                    var type_name = ''
                else {
                    var type_name = data[i].type.name;
                }
                if (data[i].person == undefined)
                    var person_name = ''
                else {
                    var person_name = data[i].person.name;
                }
                if (data[i].location == undefined)
                    var location_name = ''
                else {
                    var location_name = data[i].location.name;
                }
    
                dataSet.push([
                    data[i].id,
                    data[i].device,
                    type_name,
                    data[i].additionalInfo,
                    data[i].serialNumber,
                    person_name,
                    location_name
                ]);
            }
    
            $('#data-table').DataTable( {
                data: dataSet,
                columns: [
                    { title: "Id" },
                    { title: "Device" },
                    { title: "Type" },
                    { title: "Additional Info" },
                    { title: "Serial Number" },
                    { title: "Person" },
                    { title: "Location" }
                ]
            } );
    
      }
    });
    

    }

  • colincolin Posts: 15,144Questions: 1Answers: 2,586
    Answer ✓

    Yep, there's no mention of items, so do something like this data[i].device (rather that data.items[i].device)

This discussion has been closed.