data.items is undefined; can't access its "length" property!
data.items is undefined; can't access its "length" property!
dos4ever
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:
This discussion has been closed.
Answers
I would guess that the
data
parameter passed to your success function doesn't have anitems
object resulting indata.items
being undefined. Without seeing whats indata
its hard to say. You can useconsole.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
From the error, I'm guessing the
data
object doesn't contain another object calleditems
...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() {
}
Yep, there's no mention of
items
, so do something like thisdata[i].device
(rather thatdata.items[i].device
)