Cannot update item when querying in hook

export function Visits_afterUpdate ( item , context ) {
//TODO: write your code here…
let hookContext = context ; // see below
// let facilitator = item.facilitator
// wixData.query(“User”)
// .eq(“firstName”, facilitator)
// .find()
// .then((results)=>{
// if (results.items.length>0){
// item.facilitator=results.items[0].user

//     } 
// }) 
// wixData.update("Visits", item); 
// return item 

**if** ( item  ==  **null** ){ 
    **return**  **null** 
} 
**else** { 
    console . log ( "Visits_afterUpdate" ) 
    // getWorkshopCountForVisits() 
    **let**  number ; 
    **let**  school  =  item . school ; 
    console . log ( school ) 
    wixData . query ( "Workshops" ) 
    . eq ( "school" ,  school ) 
    . find () 
    . then (( results ) =>{ 
        // console.log("number: "+results.totalCount) 

        console . log ( "workshops: " + results . items [ 0 ]. title ) 
        item . workshops  =  results . items . map ( **function**  get_ids ( i ) { 
            i . _id 
        }) 
        
        item . numberOfWorkshops  =  results . totalCount 
        console . log ( "number: " + item . numberOfWorkshops ) 
        
    }); 
    // wixData.update("Visits", item) 
    console . log ( "before return:" + item . numberOfWorkshops ) 
    **return**  item 
} 

}
I would like to keep the change in the query when I return the item. I’m not really sure why it go through the code but when it returns, it is undefined. Can someone help me with this?

export function Visits_afterUpdate(item, context) {
    let output={};
    
    output.incommingItem = item;
    output.incommingContext = context;  
    
    if(item === null){return null}
    else{console.log("Visits_afterUpdate")
        // getWorkshopCountForVisits()
        
        let schoolID = item.school; console.log(schoolID)
        return wixData.query("Workshops")
        .eq("school", schoolID).find()
        .then((res) =>{
            output.res = res;
            output.items = res.items;
            output.firstItem = res.items[0];
            output.workshopTitle = res.items[0].title;
            output.numberOfWorkshops = res.totalCount;
            //--------------------------------
            output.workshops = res.items.map(function get_ids(i) {i._id});
            return output;    
        });      
    }
}

I can’t use the code provided. The multi-reference field won’t save if I treat it as a normal array. Although it says “all changes saved”, the change will disappear if I close the view of the collection and reopen it. I’m using insert reference instead but it’s working half of the times. Also, I can’t use remove referen/ce because it will run out of time quota.

Here is my code

Yes, this was just an example to make it possible to show you all the console-logs, on your front-end. This way you also get all the logs on front-end, without using the BACKEND-TEST-ENVIRONMENT to test your BACKEND-CODE.

Did you got some results on FRONT-END ?
Use these results to generate your → resulted item-object ←
Now you should see better what you are doing and which results you get back.
The more you see whats happening → the faster you will get your final result.
A simple technique, which will help you to code faster on BACKEND.

And yes you will have to use → item <— and returning its modified version back to frondend.

Simple example…

…bla bla bla code
…bla bla bla code
…bla bla bla code
.find()
.then( async (res)=>{
//defining → “items”
let items = res.items;

//start of ITEM-MODIFICATION.....-->  **DIRECT-MODIFICATION** 
items[0].title = "xxxxxxxxx"; 
items[1].title = "yyyyyyyy"; 
items[2].title = "zzzzzzzzz"; 

//...in your case --> you have another function running first..... 
// ---> **INDIRECT-MODIFICATION --> calling a further function (wait first for a further process)....** 
console . log ( "workshops: " + results . items [ 0 ]. title ) 
let modifiedItems =  **await**  items . map ( **function** get_ids ( i ){ i . _id }); 
console.log("Modified-Items:  ",  modifiedItems ); 

//end of ITEM-MODIFICATION... 

}).catch(…
…bla bla bla code
…bla bla bla code
…bla bla bla code

Maybe that is what you were searching for.