Cannot get the results to returned back to my page code function from public .js file function.

I have cut the code down to the core problem I am struggling with. Any help very much appreciated as I tried lots of things now and it feels like it should an easy solution. Though it’s all a learning curve to me. I found the solution now and updated it here. Return on the async function was missing.

Import {fetchRecordItem} from ‘public/BackOffice.js’
import {SearchDatatbase } from ‘public/BackOffice.js’
//searchKey passes the value of event.key pressed in the input box (not shown)//

$w.onReady(function () {
searchDatatbase ( ‘BookingDatabase’,searchKey)

.then((results) => {   													 
	  if (results.totalCount >0) {			//Check records found  if so then display// 
		$w('#firstNameText').text = results.items[0].firstName; 
		$w('#lasttNameText').text = results.items[0].lastName; 
	 } 
} 

});

//**********************Public BackOffice.js file ********************************//
export function searchDatatbase (dataBaseName, searchKey) {

     **return** (async function( **results** ) { 
     const results  = await fetchRecordItem(dataBaseName,searchKey); 
 return (results) 			 

    }())    
    .catch( (err) => { 
    console.log("Error :",err) 
   })  

}

import wixData from ‘wix-data’;
export function fetchRecordItem (dataBaseName,searchKey) {

   return wixData.query(dataBaseName) 
   .startsWith('firstName',searchKey) 
   .limit(1) 
   .find() 
   .then((results) => { 
	return (results) 
    }) 
   .catch( (err) => { 
        	console.log("Error :",err)     
  })  

}

Your use of the onReady() function is incorrect. See $w.onReady() :: a guide for the perplexed .

Your function should not be using the onReady() function and would look something like this:

export function InputFirstName_keyPress_1(event) {                       
   const results = SearchDatatbase ('IncallBookingDatabase',event.key); 
   console.log("results :" ,results.items[0].firstName);
}

Screen elements cannot be accessed from Public files using the $w context selector since a Public file does not have a page context. The following code that you posted will not work:

//Public BackOffice.js//
export function SearchDatatbase (dataBaseName,keypress){
	let searchKey= $w('#InputFirstName').value; // invalid use of $w

Instead, you can pass the searchKey to the SearchDatatbase() function.

Thank you for the assistance I left the onReady in by mistake when trying things. I have amended the code taking on board your advice ’ Screen elements cannot be accessed from Public files using the $w context selector’ However the problem of returning the results to the page code function still remains. I am probably doing something stupid again but still struggle resolve the issue. Thank you for your time and help I do appreciate it. Coz

@coz This line is wrong:

return wixData.query(dataBaseName,searchKey)

The .query() method only accepts a single parameter (i.e. collectionId).

Also if all the firstName values always start with a capital letter, you should make sure the search term is the same:

searchKey = searchKey.replace(searchKey[0], searchKey[0].toUpperCase());

@jonatandor35 Thanks for the fast reply and guidance. I have amended the .query() thank you. My validateKey functions takes care of presenting query
upper and lower case issue, I left it out to make it clearer. However my original problem still persist of returning results back to my page code function ? No doubt something else daft I am doing. ?

$w(‘#text1’).text =results.items[0].firstName; //Works fine to here //

return (results) //does not return results to page code?

I think I found the reason " return ( async function (results) {
results = await fetchRecordItem(dataBaseName,searchKey)

@coz Please post you amended code (and put it inside a code block to make it easy to read).