Store products filter by store collection

Hi there,
I made a page to search store products. I want to add a store collection filter to this page.
When i click Search button then call Search function. Search function call other function to get collection id, after that query store product with collection id.
But there’s something wrong with my code. In logic, it should be 3-1-2-4-5, but Wix ran 3-2-4-5-1 and can’t get collection id at 2,4,5.

Here’s my code:

export function searchButton_click() {
search();
}
export function searchBar_keyPress(event, $w) {
if (event.code === 13){
search();
}
}

function getCollectionIdByName(name) {
let collectionId=‘’;
wixData.query(‘Stores/Collections’)
.contains(‘name’, name)
.find()
.then(res => {
console.log(“1111111”+res.items[0]._id)
collectionId = res.items[0]._id;
})
console.log(“22222222”+collectionId);
return collectionId;
}

function search() {
let collectionName=$w(“#collectionDropdown”).value
console.log(“3333333”+collectionName);
let collectionId = getCollectionIdByName(collectionName);
console.log(“4444444”+collectionId);

wixData.query('Stores/Products') 

.contains(‘name’, $w(“#searchBar”).value)
.hasSome(‘collections.id’, [collectionId])
.find()
.then(res => {
console.log(res);
$w(“#repeater1”).data=res.items;

    });	 
console.log("5555555"+collectionId);    

}


Please help me fix this !!