Dataset onError problem

I am trying to catch this problem https://www.wix.com/code/home/forum/questions-answers/onaftersave-question where Tomer suggested using the dataset´s onError .

I put this code into the dataset´s onReady:

$w(“#dynamicDataset”).onError( (operation, error) => {
let errorOp = operation;
let errorObj = error;
console.log(“errorOp” + errorOp);
console.log(“errorOp” + errorObj);

The browser´s console gives me a “Save operation failed”, but the console.log is never executed. Any thoughts anyone?

EDIT It was not the question above, but this one: https://www.wix.com/code/home/forum/questions-answers/saving-erroneous-data-hang

EDIT: moe info from browser console:save operation failed: e {code: “DS_OPERATION_CANCELLED”, message: “Operation cancelled by user code. DatasetError: Operation (onError) not allowed during save”, name: “DatasetError”, stack: “DatasetError: Operation cancelled by user code. Da…wer-app/1.190.0/app.js:1:52413↵ at ”, Symbol(error was handled): true}

EDIT: I am also working on the problem as mentioned at the top of this post. I use the onItemValuesChanged and it works; if a field is edited (and lost focus), it is fired and I can set a boolean. But now it never saves anything anymore, the onError I put in for the code above is now in the way:
save operation failed: e {code: “DS_OPERATION_CANCELLED”, message: “Operation cancelled by user code. DatasetError: Operation (onError) not allowed during save”, name: “DatasetError”, stack: “DatasetError: Operation cancelled by user code. Da…wer-app/1.190.0/app.js:1:52413↵ at ”, Symbol(error was handled): true}
(anonymous) @ workerLogger.js:53
r.error @ supplementaryErrorHandlers.js:26
l @ index.js:17
(anonymous) @ defaultAdapter.js:50
tryCatch @ es6runtime.min.js:5
invoke @ es6runtime.min.js:5
prototype.(anonymous function) @ es6runtime.min.js:5
n @ app.js:27660
(anonymous) @ app.js:27660
Promise rejected (async)
n @ app.js:27660
(anonymous) @ app.js:27660
(anonymous) @ app.js:27660
c @ postMessage.es6:189
a @ wixCode.es6:207
n @ worker.js:82
self.onmessage @ worker.js:75

I am going backwards here. Could someone please have a look at this, I am stuck.

Hi, move the onError registration to be the first thing you do in the code (but still inside $w.onReady).
It seems that you’re calling it during the save process of the dataset, and it causing an error (it shouldn’t cause an error, we’ll handle it).

Tomer, you were right. I misstakenly put that onError code before the closing bracket of the onBeforeSave. You´re a life saver.

BUT… Are you working on this code right now? Because there is no more error if you save an unchanged dataset (so the onError, now in its correct location is not raised). All well, you might think, but the onAftersave is still not called, so I cannot collapse/hide my spinner.
PS On a normal save (=where there IS something to save), I show a Lightbox after I have taken down the spinner, saying something like “Data saved”.
So what now happens is this (if no changes made to dataset) and user presses Save:
0) show spinner

  1. no more errors, not in browser console, nor in logic (onError not rased)
  2. lightbox is shown saying “Data saved” (although there was nothing to save)
  3. lightbox clicked away and … spinner is still there

The lightbox is not raised in code, but in its Ón success property at design time.

I have implemented a boolIsDirty and it is set correctly, but if a onBeforeSave is registered, how do I NOT execute a Save when that boolean is false?

EDIT: just a another angle. What I could do is disable by defualt the Save button and enable it when that boolean is Dirty. So we solve at at interface level. Do you forsee any problems with that solution?
EDIT: I´ll answer it myself, it just came to me: it won´t work, the pics are the problem. the onItemChanged is not raised when you select a new pic with a button, only after the pics has been uploaded, that is the only thing connected to the collection, not the button. So you wouldn´t be able to save pics anymore.

I have a different suggestion. Instead of connecting a button to the “Submit” action, you can trigger the submit action using code. Meaning - remove the “Submit” action from the button, add an onClick event to that same button, and in the handler trigger the submit - then you can do something right after, no matter what happens. Something like this (note you need to change IDs):

$w("#yourDatasetId").save()
	.then(() => $w("#spinner").hide())
	.catch(() => $w("#spinner").hide())

This will hide the spinner when the save finishes, whether successfully or with an error.
Will that better suit your case?

Tomer, that´s a thought. I will work on it the day after tomorrow and see what I can come up with. For now, we have gone into ‘Ho, ho, ho’-mode. On Boxing Day I will pick up. I honestly have no idea if you celebrate x-mas, but I wish you all the best anyway. Hat tip for your incredible level of support.

Thanks, I really appreciate it. Happy holidays!

A quick question. The error object returns (correctly) an error in certain cases. I see a string in the browser´s console, the Error returns Object as typeof, but I cannot get to that string. .message certainly doesn´t work. So how do I extract the string from the error obj. Your doc refers to Mozilla, so is a bit sparse.

EDIT: Solved. It IS .message

Before yesterday, both a Save on a form without changes and a Save with a validation error would return an error. Now the first works a bit akward, I think. There is no more error (which is OK), but the Submit is now carried out ‘half’: on the button (connected to dataset) I have set an onSuccess LightBox, which is raised and shown. But strangely enough, the onAfterSave is not raised.

I have not found the time to do it all in code, as Homer yesterday suggested, because it will take me much time to re-write my pictures code. i.e. finding out if I disconnect the Save button from the dataset, what happens to an uploaded pic: is it in the dataset or is it just uploaded (to temp) and the save will set the correct URL? I don´t know.

So that´s why I ask for the onAfterSave raise. It is more clean and straighforward (after all, it IS now Saved, just a null-save), I believe, and it would help me to not rewrite much code.

Regarding switching to code, it is exactly the same as clicking the button - you don’t need to change anything else except writing the save command. Field values will be saved, as well as uploads.