Run update query at the end

In the following piece of code, I am taking the title & ID from the post collections and I want to update the the color of post title, likecount, and few other attributes in posts_attributes collections. This is nested in a repeater. The issue I am having is that before the update query runs fully the control jumps onto the the next item in the repeater. I tried to use the async/await to make the control wait on the current item in the repeater, but I am still not having success with that. Can anyone help?

$w . onReady ( function () {
$w ( ‘#postsdataset’ ). onReady ( () => {
let color ;
let title ;
let postid ;
let count = $w ( “#postsdataset” ). getTotalCount ();
$w ( ‘#repeater2’ ). forEachItem ( async ( $item , itemData , index ) => {
let likecount = itemData . likeCount ;
let colorcount = count - index - 1 ;
while ( colorcount > 2 ){
colorcount = colorcount - 3 ;
}
if ( colorcount == 0 ) {
$item ( “#titlebox” ). style . backgroundColor = “#DE2A6C” ;
colorcount ++
}
else if ( colorcount == 1 ) {
$item ( “#titlebox” ). style . backgroundColor = “#21C8D1” ;
colorcount ++
}
else {
$item ( “#titlebox” ). style . backgroundColor = “#FFF763” ;
let value = $item ( “#titletext” ). text ;
$item ( “#titletext” ). html = “

” + value + “

” ;
colorcount = 0 ;
}
color = $item ( “#titlebox” ). style . backgroundColor ;
title = $item ( “#titletext” ). text ;
postid = itemData . _id ;
let toInsert = {
“postId” : postid ,
“title” : title
// “color”: color
};
wixData . insert ( “Posts_attributes” , toInsert )
. then ( ( results ) => {
let item = results ; //see item below
} );
$item ( “#container2” ). onClick ( function (){
let page = itemData . postPageUrl ;
console . log ( page );
wixLocation . to ( page );
});
$item ( ‘#Heart’ ). onClick (( event ) => {
console . log ( “before” , likecount );
likecount ++;
console . log ( “after” , likecount );
$item ( ‘#Heart’ ). hide ();
$item ( ‘#Heartclicked’ ). show ();
$w ( ‘#postsdataset’ ). setFieldValue ( “likes” , likecount );
$w ( ‘#postsdataset’ ). save ();
} );
console . log ( postid , title , color );
await updaterecord ( postid , color , likecount );
});
});
});

async function updaterecord ( id , color , count ) {
console . log ( id , color , count );
await wixData . query ( “Posts_attributes” )
. eq ( “postId” , id )
. find ()
. then ( ( results ) => {
let record = results . items [ 0 ];
record . color = color ;
record . likes = count ;
console . log ( record . postId , record . title );
wixData . update ( “Posts_attributes” , record );
});
return ;
}

It looks like the problem is with the insert into the Posts_attributes collection. You need to await the call so that the insert completes before the query and updates are called. Currently, the code isn’t waiting for the insert to resolve so there isn’t a record in the Post_attributes collection to find or update.

Try changing:
wixData.insert(“Posts_attributes”, toInsert)
.then( (results) => {
let item = results; //see item below
} );
To:
let item = await wixData.insert(“Posts_attributes”, toInsert);

@robmich Thanks for your reply. I tried your suggested solution. Unfortunately, now I don’t see any output of console.logs on my preview so I am assuming the control got stuck at the insert; I was waiting for over 5 min.

On the other hand, I thought of giving you more perspective into my original code. Here’s the output from my code.


I am also adding the screenshots so you can understand where these lines are located.

So, with this, my understanding is the control works as expected until it hits the 2nd console.log statement at line 9. I want it to continue and hit the line 17 as well but then instead of that it jumps back to the beginning and start hitting line 75. So, basically I want the output like this

Line 75 - 61ad0634ee8bc60017068df3 Emotional Pressures to Emotional Independence - Part 2 #21C8D1
Line 9 - 61ad0634ee8bc60017068df3 #21C8D1 18
Line 17 - 61ad0634ee8bc60017068df3 Emotional Pressures to Emotional Independence - Part 2

and so on. So, I need to make the control wait at the update query which is not happening currently. Need help here.

@feelurlifenow Very interesting that the insert did not resolve. Try this code to see if you can catch the error:

let item = await wixData.insert(“Posts_attributes”, toInsert).catch((err) => {console.log(err)});

Thanks for sharing the logs. I’m wondering if it doesn’t like that you have a .then() in the query with await. Try changing updaterecord() to this and see if that helps:

async function updaterecord(id, color, count) {
console.log (id, color, count);
let results = await wixData.query(“Posts_attributes”)
.eq(“postId”, id)
.find();
let record = results.items[0];
record.color = color;
record.likes = count;
console.log(record.postId, record.title);
// Not sure if you need to wait on this one or not…
wixData.update(“Posts_attributes”, record);
return;
}