.NET CORE 3.1 - getting Cannot read property 'length' of undefined - Urgent

.NET CORE 3.1 - getting Cannot read property 'length' of undefined - Urgent

marcasmar94marcasmar94 Posts: 9Questions: 3Answers: 1

I migrated a working project from .net core 2.1 to 3.1 and I am now receiving this error from: jquery.dataTables.min.js
Uncaught TypeError: Cannot read property 'length' of undefined
I am fetching data from the database successfully but displaying it is causing this error.

I tried with both DataTables Editor v1.9.0 and DataTables Editor v1.9.2 and with both DataTables-1.10.19 and DataTables-1.10.20

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    That normally happens when the Ajax isn't correct - either badly formatted or the number of columns doesn't match what's configured for the table. That would be the place to start.

    Colin

  • marcasmar94marcasmar94 Posts: 9Questions: 3Answers: 1

    @colin Its weird since it's the same code, nothing was changed but the Migration to .Net Core 3.1, i will further investigate and post my findings if i get this resolved

  • marcasmar94marcasmar94 Posts: 9Questions: 3Answers: 1
    Answer ✓

    The issue was from my side!
    I forgot to add:

    services.AddControllers().AddNewtonsoftJson();

    in Startup.cs for .Net Core 3.1

    After adding that, everything went back to normal.

  • dhutton@creativeone.comdhutton@creativeone.com Posts: 59Questions: 15Answers: 0

    I just came across this issue during my migration to 3.1. As soon as I migrated to asp.net core 3.1 all of my datatables stopped working. I guess their default is now System.Text.Json vs. Newtonsoft. Is there a way to use System.Text.Json? I'm not 100% sure that's my problem but I'll go ahead and load newtonsoft and see if that fixes things.

    https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.1&tabs=visual-studio#newtonsoftjson-jsonnet-support

  • dhutton@creativeone.comdhutton@creativeone.com Posts: 59Questions: 15Answers: 0
    edited February 2020

    Update - Yup, that fixed my datatables. I'm glad I saw your post, no telling how long I would've had to diagnose that one. :/

    The question still stands though, if .net core is using another default I'd rather use that than having to load a dependency. Let me know your thoughts.

    Thanks marcasmar94!

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    We don't actually reference Newtonsoft in our library - it's the controller (or perhaps more specifically the controller's framework) that uses it. Did you update your Nuget packages when switching over to 3.1?

    Allan

  • dhutton@creativeone.comdhutton@creativeone.com Posts: 59Questions: 15Answers: 0

    Yes I did and still no go. Datatables blank. We use them a lot and every one of them came up with no results. I switch back to asp.net core 2.2 pointed at the same database, fine. Switching back to 3.1, blank. No errors. I added newtonsoft package in (because you have to do that manually now with 3.x) and added the services line into my startup.cs that marcasmar94 mentioned and everything was pulling in data just fine. Normal after that. Magic.

    Same results on my home and work computer. DT pulls in results and Editor works just fine making changes.

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    Thanks for the results of your investigation. I've filed a bug to get this looked at and hopefully resolved for the next patch release of Editor. Until then, the Newtonsoft inclusion workaround seems to be the way to go.

    Regards,
    Allan

  • colincolin Posts: 15,112Questions: 1Answers: 2,583
    edited May 2020

    Just an update to say this has been resolved and will appear in the next version of Editor (1.9.3) due out later this week.

    Colin

    edited - 1.9.3 is the next release rather than 1.7.3

  • TyreekusTyreekus Posts: 2Questions: 1Answers: 0

    OMG, thanks for this post. The Editor 1.9.3 update did not fix the problem but adding the

    services.AddControllers().AddNewtonsoftJson();

    fixed the problem.

    NewtonsoftJson is does something different compared to System.Text.Json. My Json DtReponse populates data but on xhr inspection, my model would always be blank.

  • mike92117mike92117 Posts: 38Questions: 11Answers: 1

    Is anyone able to get this to work in a production (not localhost) environment? I'm using editor 1.9.4 and my jquery API calls all return no data and never reach the endpoint. It works fine in localhost with kestrel but not when deployed to IIS server.

  • mike92117mike92117 Posts: 38Questions: 11Answers: 1

    Never mind on my last comment. Figured it out.

This discussion has been closed.