Return Value from function

Hi all ,

I am trying to get the array returned from the function . but i cannot get the return values from the function


// My Wix main code  for calling  Country detail public function
import {CountryDetail}from 'public/CountrySearch.js';

$w.onReady(function () {
	var king=[];
	console.log("ready");
	king=CountryDetail("#selection1");
   // i added this to catch the return values
console.log(king);
});

the public function

import wixData from 'wix-data';

export function CountryDetail(SelectionName)
{
var TotalCount;
	wixData.query("CountryDetail")
		.find()
		.then((results) => {var test = [];
			console.log("*****--------*****");
			let item = results.items;
			TotalCount = item.length;
			console.log("item length", item.length);
		
		for (let i = 0; i < TotalCount; i++) {
			   test[i] = {
				label: item[i].name,
				value: item[i].country_code.toString()
			};
		}
		$w(SelectionName).options = test;
               return(test);
               //  this is not throwing the values back to the calling statement;
         	});
 }

anyone can help on this .
I am not sure where i am wrong . i want to use the return array in my geomap

Regards
jap

Hey there,

At least one problem jumps out right away. You need a return before wixData.query( ):

var TotalCount; 	
return wixData.query("CountryDetail")
  ...

This has to do with JavaScript Promises. The return(test) is not a return on the function, instead it is a resolution of the Promise returned by the query.