Data Not Processed On Edit In Node
Data Not Processed On Edit In Node
daduffy
Posts: 31Questions: 5Answers: 1
I have a Node datatables editor project setup. I can create records, however, I cannot edit records.
The data is passed into Node, I have set the urlencode to true, but It doesn't appear that anything happens.
I am using versions:
Datatables 1.10.19
DataTables Editor v1.9.0
Here is the section of my code for debug:
let editor = new Editor( edb, 'tbl_company', 'companyid' ).fields(
new Field( 'company' ),
new Field( 'address1' ),
new Field( 'address2' ),
new Field( 'city' ),
new Field( 'province' ),
new Field( 'postal' ),
new Field( 'country' ),
new Field( 'officephone' ),
new Field( 'faxphone' ),
new Field( 'creationdate' )
.validator(
Validate.dateFormat(
'YYYY-MM-DD',
null,
new Validate.Options({
message: 'Please enter a date in the format yyyy-mm-dd'
})
)
)
.getFormatter(Format.sqlDateToFormat('YYYY-MM-DD'))
.setFormatter(Format.formatToSqlDate('YYYY-MM-DD')),
new Field( 'notes' ),
new Field( 'hide' )
);
console.log( 'req.body:' ) ;
console.log( req.body ) ;
await editor.process( req.body ) ;
console.log( 'editor.data():' ) ;
console.log( editor.data() ) ;
Those two console logs produce:
req.body:
{ action: 'edit',
data:
[ { companyid: '15',
company: 'ABC Corp',
address1: '123 Fake Street',
address2: '',
city: 'Somewhere',
province: 'ON',
postal: 'A1B2C3',
country: 'Canada',
officephone: '7058675309',
faxphone: '',
creationdate: '',
notes: '',
hide: '1' } ] }
editor.data():
{ data: [], fieldErrors: [] }
Any direction that anyone can provide would be greatly appreciated.
Dave
This question has accepted answers - jump to:
Answers
I forgot the link to the test https://salto.as5.co/db/company
Hi @daduffy ,
The data in the console log above is making it back to the Editor client - it's being sent:
It should be receiving the modified data back from the server. After your
editor.process()
, I think you have to send the data back to the server with:Hope that does the trick,
Cheers,
Colin
Hey @colin ,
Thanks for the reply. I am sending it back that way with the res.json, I was just trying to post the part of my code where I thought the problem was, I truncated the paste too early. Sorry about that.
The issue appears to be in the editor.process.
If I create a record, everything looks good, however, nothing happens on an edit. I put a link to the project in the previous link if that helps at all.
Dave
Hi Dave,
In your Knex configuration could you add
debug: true
so we can see what SQL is being generated please? It will dump it out onto the console (assuming you are running your script from there).Also add
editor.tryCatch(false);
before the process call. I've got a feeling that there is an SQL error happening and the error is being thrown away.Thanks,
Allan
@allan
Well, I thought it was an SQL issue too, and it does appear to be that the query is where we are breaking.
All of the data is escaped, I don't know if that is a problem or not. In any event, the id is not 0. I don't know where that is coming from.
Here is my editor server script
Let me know if there is anything else you need.
Thanks again for all of your help.
Dave
So, I am not sure that the server is picking up the rowid. From console dumps, all of the data is passed, if I dump
the editor object, it has brought in the formdata just fine.
The editor object recognizes that the pkey is companyid. I have messed around with changing the idprefix to something different, nothing, and default, and that doesn't appear to help. I have changed it both on the client and server sides.
It is almost as if the process of changing the bodyparser so that the object can be read eliminates the rowid dats. I think this because it works just fine on an insert, and that is the only real difference I see in the recieved data when I compare my node server script to my php server script.
Am I off, or could I be on the right track?
Dave
Hi Dave,
I think you are right - I missed that in the original JSON that shows your req.body. It doesn't have an id for the row, which it really should. So a few things:
Thanks,
Allan
@allan
I use the same table throughout my project and call a routine to define the tableDef object that you see below... I just did a dump of what it is producing so that you can see how the editor and table are defined. I changed a couple of details so as not to expose everything in this post for all to see forever.
Request Headers
Response Headers
Thought I'd throw in the form data for good measure
Also @allan is the donate button the best way to send some sheckles to you for all of your help?
. You've got an Editor license - that's plenty enough thanks!
This is what the client is sending to the server:
So the question becomes, why is that
14
being dropped?How are you setting up
body-parser
? In the demos (Express) I use:Thanks,
Allan
I am using it directly in Express, from the docs it looks like that is the future of things. I will add the other library and try it that way.
@allan
Alas, that didn't do the trick. Everything is as before. Of note: when I dump the post data on the server side is that I cannot find the row id anywhere. The data object doesn't mention it, sort of like the act of parsing it with extended removes that reference to the data's key.
Is there a workaround that might work? I can look through the code, but is there a routine that I can call that will allow me to set the row id manually?
Thanks again for all that you guys do!
Dave
What version of body parser are you using? I don't know of a workaround for this I'm afraid, as I don't recall having seen something quite like this before. The client-side is sending the correct data to the server, but the server-side isn't parsing it correctly.
Can you also show me your full server-side Javascript?
Thanks,
Allan
@allan
I installed version 1.19.0 of body parser.
Here is my app.js file:
Here is my editor.js file:
Here is my knex db file, edb.js:
Let me know if there is something else you need.
Thanks again!
Dave
@allan
Also, I should add this is the dump from calling at preEdit:
I don't know if that helps narrow it down, but that is where I am at with troubleshooting.
Dave
Hey @allan @colin
Please don't misunderstand, I really appreciate all of the help you provide.
Can I buy some support credits to get through this issue faster? I know that you said my purchase of Editor is enough, but I also know that nobody can work for free. Again, please don't take this as me being at all ungrateful for the piles of help already.
How would be the best way to go about this?
Thanks!
Dave
Interesting - I'm not seeing anything immediately that would cause this issue. You are modifying
req.body
which is setting of my spiddy-senses, but it looks like it should work okay (I'd preferfor ... of ...
rather thanin
though).Could you add:
at the very top of the route handler, and also again just before the
process()
method call, and then show me the output from the console?Thanks,
Allan
@allan
You didn't answer my question about support credits.
Here is the output from the top of the route:
Here is the output just before process:
Just a background on the changing of the data.... a while ago I had a problem in the link here, that is why I transform any strings to numeric that need be, there aren't any boolean, but I transform them now. I did try without any transformation, the problem is still the same.
https://datatables.net/forums/discussion/55324/load-state-not-honouring-data
Thanks!
Dave
@allan
I figured out the problem, and came up with a workaround in my server script. However, there still is an underlying problem that hasn't been fixed. Further, my workaround only addresses single row edits.
In Editor's class there is the function _process which sets the id. The id is pulled from the req.body.data's key. However, when the bodyParser is set to bodyParser.urlencoded({ extended: true }) it strips the data's other keys and it is reduced to keys = '0'.
In your editor.js file, the areas where the id is set is:
When I searched, I found that the keys was always '0', since the req.body.data had been transformed by bodyParser.
I added this to my server script before the process call:
Of note to anyone else reading this thread, this workaround WILL NOT work for multi row edits. My project will only ever have single line edits, so this is not an issue for me.
@allan @colin I truly appreciate all of your help, you guys are the best!
If you would like anything else to help sort out this problem for anyone else, by all means, let me know. Feel free to email me if you would like any sensative data as well; I am open to sending you access to my whole project.
Thanks!
Dave
Awesome to hear you've got a workaround. I've just sent you a PM to see if we can get to the bottom of the underlying issue.
Allan
Not sure how to switch the answer, @colin or @allan , but I actually figured out this problem while working on a new project. I wanted to post the answer here, in case anybody was every dealing with this issue in the future.
Turns out the problem was in the bodyParser, as we suspected.
The following line of code was causing the problem.
I am not an expert on the Express bodyParser, but the tool, it looks like, was stripping out the req.body.data's parameter, since there was only one, and bringing it down to just a flatter level... exactly what it is supposed to do.
The bodyParser.json did this a little more that the urlencoded, and that cleared out the ID when it went into the server. By removing the json part of it, it actually leaves it in there.
Not sure if more light needs to be shed on a 1.5 year old problem, but here is an actual solution, as opposed to just a workaround.
Nice, thanks for remembering and reporting back!
Colin