Upgraded from 1.9 to 2.0.5 and I now get an error on create.
Upgraded from 1.9 to 2.0.5 and I now get an error on create.
I am sure you will need more info, but I am only able to debug so much into the DataTables code without understanding what I am looking at.
After updating, my create results in "Cannot read property 'Id' of null". I am using the idSrc: "Id"
option. When my create POSTs to the server, it returns the newly created object, and I have verified that it DOES have an Id set.
I noticed in the changelog, the create on server-side should now highlight a row? I assume this fix/feature resulted in the breaking change I am now seeing.
Please advise on any further troubleshooting or information I can provide. My codebase is large, and my table is all server side with custom .NET code (not the editor .NET library), so providing an example is difficult. I was hoping this is a known issue.
This question has an accepted answers - jump to answer
Answers
This is my custom ajax create function. I share this across a few tables. Also, I found out some of my other tables work. So I am still digging.
Still digging, but I have narrowed the issue further. The error only happens if I submit JSON to my server with a property that has a null value.
The full stack trace:
I isn’t something we’ve had a report of already I’m afraid (at least, not that I recall). So no quick fix sadly.
Can you show me the data that is being sent to the server, the response from the server and also the Editor Javascript configuration object?
Thanks,
Allan
Request to Server:
POST
Response from Server:
Editor Configuration:
I think I got it.
I had a
postSubmit
function doing this:My fix is:
Is there a better way to do that?
Not sure why this worked in DT 1.9.6 and not in 2.0.5
Interestingly, your new code doesn't change the
json
object that Editor sees at all. Rather it is assigning the local variablejson
a new object. That is not being returned, and so Editor won't make use of it at all.What you'd actually need to do in this case is put a modifier into the Ajax
success
callbacks that you have. What doesDataTableAjaxComplete
contain?Allan
If it works without me changing the json object, am I good to go? I guess it just wont be able to figure out the id of the newly created row, but it does not throw any errors in the console.
@allan did some code cleanup, I just removed the postSubmit mapper and moved the code you suggested over into the success callback of the create ajax option. Same error though:
Error
Editor Options
dataTableAjaxComplete function
Request
Response
Unfortunately, that all looks quite sensible and like it should work. Could you give me a link to your page so I can debug it directly please?
One possible thing you might need to add to the
$.ajax
call isdataType: 'json',
but jQuery should really detect JSON automatically.Allan
@allan I figured it out
Works in 2.0.5
No longer works in 2.0.5
it would take me a while to get you a copy of the site since its behind our VPN, so I just downloaded the nodejs sample and started hacking away.
If you want to reproduce this quickly, download the nodejs sample, edit any page, say fieldDefaults.html, remove
ajax: "/api/staff",
and instead change it to:You will see the error. I used sqlite to quickly populate the DB and test.
Also, regarding "it only works when I do not send any null value properties" this is true:
Works
Fails
Ah! Very interesting - thank you for spotting that and letting me know about it. That's an artefact of how Editor worked in v1.0-1.4 where only a single row at a time could be edited. With 1.5 we introduced multi-row editing which meant an array must be returned.
With v2 I removed the support for the old style single row, and it has impacted this as well.
I can add a check and correction into Editor if that is going to be a problem for you to change in your scripts?
Allan
@allan No need to add that. I have one global editor configuration object that I can override the ajax data property for all CRUD operations on my end.