Database update sporadically fails

A bit of my code in BackendWeb.jsw seems to work reliably when it unconditionally makes a query, adjusts the value of a field, and writes back the result. When a conditional is added, it seems to work reliably the first time the page is opened, but only sporadically after that. The error is “Cannot read property ‘length’ of undefined” and is thrown on the line marked ***. All the fields in the database are text, so it is not a problem with data type.

export async function UpdateTasks (ToUpdate) {
let options = {
“suppressAuth” : true ,
“suppressHooks” : true
};
let success = false ;

let results = await wixData.query( “Tasks” )
.eq( “title” ,ToUpdate.record)
.find();

//-------This doesn’t always work ---------------------------------------------------------------------
let nonzerolen = (ToUpdate.value.length > 0 );

*** if ((results.items[ 0 ][ToUpdate.field].length > 0 ) && nonzerolen ) {
success = false ; //someone else got there first
} else {
results.items[ 0 ][ToUpdate.field] = ToUpdate.value;
let taskstatus = await wixData.update( “Tasks” ,results.items[ 0 ],options);
success = true ;
}

return success; 

//---------This seems to work reliably ------------------------------------------------------------------
/*
results.items[0][ToUpdate.field] = ToUpdate.value;
let taskstatus = await wixData.update(“Tasks”,results.items[0],options);
return true;
*/
}

I’m guessing that “await” is not doing what I expected it to do, and yet if that were the case why would the update not fail?? One other thing - a message about a problem with a promise sometimes appears when trying to open the page in Preview. I guess the error leaves something that needs to be tidied up, but I’m not sure how to tidy it up.

Try another structure using —> .then(()={ });

exportasyncfunction UpdateTasks (ToUpdate) {  
	let options = { "suppressAuth": true,  "suppressHooks": true};  
	let success = false 
	
	return wixData.query("Tasks")         
	.eq("title",ToUpdate.record)         
	.find()
	.then(()=>{
		//...continue here your code.....
	});
	return //.....here the end-return
}

Thanks for your reply. I tried the more standard .then approach originally, but ran into problems for other reasons.

@mike13529
Which ones?

@russian-dima The query is one task that returns a promise, and the update is another nested within it. When the functions returns, both need to be completed before refreshing the dynamic dataset so it now displays the newly-entered value. Putting a .then before the refresh causes it to update when the query has completed, but not before the update has completed so the new data does not show.

@mike13529
Tried to recunstruct, but because of leck of informations, could not find an end-solution.

import wixData from 'wix-data';

export async function UpdateTasks (ToUpdate) {
   let success
   let value = ToUpdate.value
   let options = {
      "suppressAuth": true,
      "suppressHooks": true
   };
 
   return wixData.query("Tasks")
   .eq("title",ToUpdate.record)         
   .find()
   .then((res)=>{console.log(res)
       let items = res.items
 
       if (items[0][ToUpdate.field].length>0 && ToUpdate.value.length > 0) {
            success===false
       }
       else {
          items[0][ToUpdate.field] = value;
          wixData.update("Tasks", items[0], options)
          .then((x)=>{
             console.log(x)
             let taskstatus //??????????
             success = true;
          })        
      }
      return success
   });
}

How do look like your —> “toUpdate”-package? What is inside?

@russian-dima ToUpdate has three elements .record, .field and .value

I think the trouble has something to do with chaining promises, and then passing a promise back to the calling code properly. From my limited understanding I thought that promises were supposed to be chained automatically, but the .then code does not seem to be behaving as if they are. That was the reason for switching to the await approach, which did seem to be an improvement (for a while).

Hi Vlad - sorry, it occurred to me while getting ready to go out that I had not made the problem with the .then version sufficiently clear. It works fine in terms of the database. The problem is to get it to return a promise that only resolves when the update has been done. Ideally, that would be a promise of a return success status true/false depending on if there has been a clash between users or not.

PS - why did I call you Vlad???

@mike13529 Idk. :laughing:
I will take a look on it the next days, when i find more free-time.