I’m calling a function in BackEnd.jsw to write some data to get round user permissions, and then using code in the page to update a dataset after the data has been written. Async / await looked like a good way round it, but it falls foul of the 14 second rule in the real application - it turns out that database actions using async / await are sloooooow.
So, I’ve written a small test function that accepts a value from an Input box and writes it to a record and field populated from the onClick event of a table containing data. The value is supposed to appear in the table when it is subsequently refreshed by the code.
/*
Front end code
*/
export function btnSet_click(event) {
let ToUpdate = {
"record": ("00" + (displayData.row).toString(10)).substr(-2),
"field": displayData.column,
"value": $w('#inputValue').value
}
console.log(ToUpdate);
UpdateData(ToUpdate)
.then ((updated) => {
if (updated) {
$w('#dynamicDataset').refresh();
console.log("Refreshed");
} else {
console.log("No update needed")
}
});
}
/*
Backend code
*/
export function UpdateTable (ToUpdate) {
let options = {
"suppressAuth": true,
"suppressHooks": true
};
let updated = false;
//Funny place for a return just below, but necessary because the arrow function is, well, a function
//Get can be used like this because records were created with _id = 00 .. 20
return wixData.get("DataTable", ToUpdate.record)
.then ((updateRecord) => {
// Check if the required value is already there - for example, clicked Set twice. If so, no further action
let existingValue = updateRecord[ToUpdate.field];
if (existingValue == ToUpdate.value) {
updated = false;
return updated;
}
// If necessary, do the update
updateRecord[ToUpdate.field] = ToUpdate.value;
wixData.update("DataTable",updateRecord,options);
updated = true;
console.log("Updated");
return updated;
});
}
The good news: value of updated is properly returned
The bad news: the console log shows “Refreshed” before it shows “Updated”
In my limited understanding, wixData.get returns a promise so the .then in the front end should not fire until the data has been updated. But the console log suggests otherwise.
Do I need to do something to make updated, as returned by the back end code, into a promise?
Or what else did I get wrong??