Get a backend query result

I need to find the ID of myCollection. I have the value of other field (Id_sqlSel) of this collection. So i wrote this backend function:

export function findId(myCollection, Id_sqlSel)
{
wixData.query(myCollection)
.eq(“id_sql”, Id_sqlSel)
.find()
.then ( results => {
let items = results.items;
let item = items[0];
let idCollection= item._id;
return idCollection;
})

}

I need to get the idCollection when I call the function from this page function:

export function buttonPruebaFuncion_click(event) {
let IdSel = parseInt($w(“#idInput”).value, 10);
findId(“TempPeople”, IdSel)
.then( function (findId) {
console.log(findId);
$w(“#inputResultados”).value = findId;
});
}

… but I only get an undefined value.
Can any of you explain what I’m doing wrong?
Thank you,
Arturo

Hi,
Try to return the query itself, meaning:

export function findId(myCollection, Id_sqlSel) {   
  return wixData.query(myCollection)         
            .eq("id_sql", Id_sqlSel)         
            .find()         
            .then ( results => {  
                    let items = results.items;  
                    let item = items[0];  
                    let idCollection= item._id;  
                        return idCollection;         
                })  
}

Good luck!
Tal.

@Tal: I’ll try your code, and again thank you for all your collaboration!

Hi, I saw this discussion and I apologize if I intrude.

I had a problem with the late return of the promise so I entered the return, but if I put return in a backend file it doesn’t work gives me an error in Live.

There is some reason
Thanks for your help