I need some help with able to return back myArray from the function call seachResults2. When I do a console log the compiler tells me it cant find myArray. I want to be able to return myArray to the caller.
const searchResults2 = (query: string) => {
const subscriptionKeyCredential = new atlasService.SubscriptionKeyCredential(
"SUBSCRIPTION"
);
const pipeline = atlasService.MapsURL.newPipeline(subscriptionKeyCredential);
const searchURL = new atlasService.SearchURL(pipeline);
searchURL
.searchAddress(atlasService.Aborter.timeout(10000), query)
.then((response) => {
const data2 = response.geojson.getFeatures();
const myArray = new Array(data2.features.length)
.fill(0)
.map((item, idx) => {
const category: string = data2.features[idx].id as string;
return {
value: category,
label: labelElement(query, category),
};
});
});
console.log(myArray); // Cannot find name 'myArray'
//return myArray cant do since its not accessable.
};
const labelElement = (query: string, category: string) => {
return (
<div
style={{
display: "flex",
justifyContent: "space-between",
}}
>
<span>
Found {query} on <a>{category}</a>
</span>
<span>{getRandomInt(200, 100)} results</span>
</div>
);
}
And its been called like this.
const [options, setOptions] = useState<SelectProps<unknown>["options"]>([]);
const handleSearch = (value: string) => {
setOptions(value ? searchResults2(value) : []);
};
This is a question regarding scope of variables.