0
votes

I've setup datatables.net with server side processing on a MVC application using the DataTables.Aspnet.Core & DataTables.AspNet.WebApi2 nugets. All works well and the data gets loaded and displayed fine.

If there is an issue on the server side I catch the exception and return a result containing my custom error message:

// IDataTablesRequest tableRequest
return new DataTablesJsonResult(Response.Create(tableRequest, "my custom error message"), Request);

On the client side I've registered to the error event as well:

$.fn.dataTable.ext.errMode = 'none';
this.$dataTable.on('error.dt', this.onDataTableError.bind(this));

This also works fine, I can parse the response and check for my custom message.

Now the issue: After handling the error I always get the dreaded Uncaught TypeError: Cannot read property 'length' of undefined I assume this is the case because I don't send any data to the client?

How can I prevent this error?

1

1 Answers

0
votes

I could not manage to fix this on the server side by adding an empty dataset as this would not be passed with the json.

What I ended up doing is ensuring there is always a data array on the client side:

// register to ajax request event
this.$dataTable.on('xhr.dt', this.afterAjax.bind(this));

// set empty array to make sure we have no issue 
// with null references on data.length for loop
afterAjax(event: JQueryEventObject, settings: any, payload: any, response: any): void {
        payload.data = payload.data || [];
}