(EDITED - At bottom of post)
A little background info - I was testing the use of my website’s service, and found an issue where if multiple accounts (2+) executed the same function at around the same time, both saves would be under whoever executed the function first.
So - I thought maybe a queue would help, and wrote up a test queue script, both front and back end, and encountered the same problem testing on 2 accounts. This is what i ended up with
Front -
import {backendTest} from "backend/Test.jsw";
import wixUsers from 'wix-users';
export function queueBtn_click(event) {
console.log("pressed");
backendTest(wixUsers.currentUser.id);
}
Backend -
import wixData from 'wix-data';
import wixUsersBackend from 'wix-users-backend';
var queue = [];
export function backendTest(id) {
queue.push({"Msg":"Item In Queue","PositionOnEntry":queue.length, "owner":id});
if (queue.length === 1){
DoThing();
}
}
function DoThing(){
if (queue.length > 0){
var item = {
'object': queue[0]
}
setTimeout(()=>{
queue.shift();
wixData.save("test", item).then(()=>{
DoThing();
});
},2000);
} else {
var endMsg = {
'title' : 'Queue Ended',
}
wixData.save('test', endMsg);
}
}
And the result from 6 button clicks each on two account within a minute is -
As shown in the picture, the owner Id is consistently a single account, however the real owner saved in the saved object.
I have no clue outside of a workaround how to fix this - I have repeated the same and slightly different tests several times with the same conclusions.
Back to the question - Given what is occurring, to prevent the same situation in my site’s service occurring, should i save the “real owner” in its own separate field? or is there a fix for this?
Thanks in advance.
(EDIT) This is the results with adding Auth suppression and setting the _owner:
import wixData from 'wix-data';
import wixUsersBackend from 'wix-users-backend';
var queue = [];
let options = {
"suppressAuth": true
};
export function backendTest(id) {
queue.push({"Msg":"Item In Queue","PositionOnEntry":queue.length, "owner":id});
if (queue.length === 1){
DoThing(id);
}
}
function DoThing(id){
if (queue.length > 0){
var item = {
'_owner': id,
'object': queue[0]
}
setTimeout(()=>{
queue.shift();
wixData.save("test", item, options).then(()=>{
DoThing(id);
});
},2000);
} else {
var endMsg = {
'_owner': id,
'title' : 'Queue Ended',
}
wixData.save('test', endMsg, options);
}
}
Results from 6 clicks
As you can see, passing through an Id and setting the owner helped for the first two saves but then it just goes back to doing what it was doing before. Any ideas?