paul_tanner Posts: 10Questions: 1Answers: 0
This my first attempt at an editor.
Error messages shown: none
Description of problem:
This creates an editor and shows me the incoming data.
I click a cell and the Edit button. A box appears and I change the data then click "Update".
The screen goes back to showing the original data and the postEdit event does not fire.
I looked for a complete simple example but did not find one.
Sonds like your server script isn't returning the updated data as descried in the Editor client/server docs. Start by using the browser's network inspector to see what is returned by the server script when editing. Also check for errors in the browser's console. Let us know what you find.
Thanks @kthorngren for the quick reply.
But I'm still confused.
I haven't yet written the server side code for an update.
This is because I don't know how to configure the update.
The link you gave gives an idea of what the data will look like. I was hoping just to display that in the browser console to confirm the format.
Then I need to actually send it.
In my code I mention the server address twice. I'm wondering if the second reference needs to be a put rather than an ajax get?
That's the problem. You've Editor initialisation has:
Which is telling it to submit the form data there and it expects some JSON back. I'm guessing it is returning valid JSON (since otherwise it would give an error), but exactly what that is - perhaps the same as the original DataTable load?
Are you planning on using our PHP libraries for Editor? If so, it will handle the create / edit / delete actions for you.
Editor will send a POST by default. DataTables sends a GET by default. If you are using our PHP libraries for Editor, that will work just fine.
If you aren't - you need to look for the POST data when the form is submitted to check what action is being performed based on the
actionproperty. The page Kevin linked to gives full details for the client / server communication protocol.
OK. Made progress.
Now I say:
In the express app at 9090 I have
No when I edit req produces a lot of output but there's nothing in req.data
Evidently still misunderstanding something.
whoops. I should have written
this gives a response but I'm not clear on how to parse it.
I also note that it does not contain a row id
I added a content-type header to the post request but it still produces this unparsable response. Any further suggestions please?
req.body.datawill contain the
dataobject that Editor sends to the server. You'll need also
req.body.actionto know what action is being performed.
Then loop over the
req.body.dataobject (since it is an object that contains the row ids) -
Object.entries()is useful for that with a
for ... inloop.
Is there something you need to do that the Editor NodeJS libraries won't handle for you? It would save you writing all the database interaction code!
My plan was (after initial testing) to integrate it into an existing express application.
The data looks ok on presubmit
(Except that the index field for a single-row update is missing,)
The issue seems to be that the req.body.data object when received at the server is not an object that can be parsed. It looks like it got stringified or something.
Neither req.body.data nor req.body.action are available in app.post (they did not get parsed out).
That was why I was looking at the headers.
I have at last seen that I was not referencing my table in Editor
Now we have this on presubmit:
but there is still no request.body in the express post
That suggests to me that the body parameters aren't being parsed in your node server. How are you parsing them? You'll need something like:
That is for Express - other frameworks will have something similar.
If I manually post that to my express application it is accepted without a problem.
However, if I let Editor post it I get this from express (I added some error handling) :
Just to recap on the call:
I'm not sure what else I can check to see how between correct json on presubmit and fault json received this is failing.
OK. I think I found part of the problem.
This "works" when express is expecting url encoding:
This does not work when express is expecting json encoding
To my mind this is illogical.
Anyway, in the first case the request body does not translate properly from
on presubmit to
In which the id has been omitted so the DB cannot be updated.
So, I need to know why the editor sends url encoded data when contentType is set to 'application/json' is specified?
Very interesting - thank you! It isn't something that Editor itself is doing, but we just proxy all the Ajax requests through
jQuery.ajax()and it looks like it does some smart handling of the
contentTypeparameter, to change the data that is being submitted based on the value you give. It makes sense that if you tell it to submit
application/jsonthen it will send data in that format. That could be decoded in the Node application, or you could continue using body parser without that parameter.
OK. I bodged it so that the message sent with url encoding is decodable and include the row id.
So now the whole example works. In the event of an error in an update it would be good to signal that. Can.t see how to do that.
The JSON return from the server should include the
errorparameter for a general error, or a
fieldErrorsarray in the case of one or more fields being in error (e.g. validation errors) - full details available in the documentation here.